mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-12 00:51:08 +00:00
tweaks for url loading
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@25142 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
7c633ab823
commit
1d562005d1
9 changed files with 259 additions and 240 deletions
|
@ -5,9 +5,11 @@
|
|||
* Source/win32/NSStreamWin32.m:
|
||||
* Headers/Foundation/NSStream.h:
|
||||
First hack at extensions to get address and port properties for
|
||||
network streams.
|
||||
network streams. Use localhost if no host is given for connect.
|
||||
* Source/GSFFIInvocation.m:
|
||||
Attempt to use forward2 if available.
|
||||
* Source/NSURLProtocol.m:
|
||||
Fixup some problems with incorporation of code from mySTEP.
|
||||
|
||||
2007-05-11 Richard Frith-Macdonald <rfm@gnu.org>
|
||||
|
||||
|
|
|
@ -442,6 +442,7 @@ extern "C" {
|
|||
|
||||
|
||||
#include <Foundation/NSURLHandle.h>
|
||||
#include <Foundation/NSURLConnection.h>
|
||||
|
||||
@class NSArray;
|
||||
@class NSDictionary;
|
||||
|
@ -504,13 +505,10 @@ extern "C" {
|
|||
@interface GSXMLRPC : NSObject <NSURLHandleClient>
|
||||
{
|
||||
@private
|
||||
#ifdef GNUSTEP
|
||||
NSURLHandle *handle;
|
||||
#else
|
||||
NSString *connectionURL;
|
||||
NSURLConnection *connection;
|
||||
NSMutableData *response;
|
||||
#endif
|
||||
NSTimer *timer;
|
||||
id result;
|
||||
id delegate; // Not retained.
|
||||
|
|
|
@ -72,7 +72,7 @@ extern "C" {
|
|||
|
||||
/** <init />
|
||||
* Initialises the receiver with the specified request (performing
|
||||
* a deep copy so that ithe request does not change during loading)
|
||||
* a deep copy so that the request does not change during loading)
|
||||
* and delegate.<br />
|
||||
* This automatically initiates an asynchronous load for the request.<br />
|
||||
* Processing of the request is done in the thread which calls this
|
||||
|
|
|
@ -52,29 +52,7 @@
|
|||
|
||||
#include "GNUstepBase/GSMime.h"
|
||||
#include "GNUstepBase/GSXML.h"
|
||||
#ifndef NeXT_Foundation_LIBRARY
|
||||
#include <Foundation/NSArray.h>
|
||||
#include <Foundation/NSBundle.h>
|
||||
#include <Foundation/NSCalendarDate.h>
|
||||
#include <Foundation/NSCharacterSet.h>
|
||||
#include <Foundation/NSData.h>
|
||||
#include <Foundation/NSDate.h>
|
||||
#include <Foundation/NSDictionary.h>
|
||||
#include <Foundation/NSException.h>
|
||||
#include <Foundation/NSFileManager.h>
|
||||
#include <Foundation/NSHashTable.h>
|
||||
#include <Foundation/NSInvocation.h>
|
||||
#include <Foundation/NSMapTable.h>
|
||||
#include <Foundation/NSRunLoop.h>
|
||||
#include <Foundation/NSString.h>
|
||||
#include <Foundation/NSTimeZone.h>
|
||||
#include <Foundation/NSTimer.h>
|
||||
#include <Foundation/NSURL.h>
|
||||
#include <Foundation/NSURLHandle.h>
|
||||
#include <Foundation/NSValue.h>
|
||||
#else
|
||||
#include <Foundation/Foundation.h>
|
||||
#endif
|
||||
|
||||
/* libxml headers */
|
||||
#include <libxml/tree.h>
|
||||
|
|
|
@ -66,7 +66,7 @@
|
|||
- (void) _setHeaders: (id)headers;
|
||||
- (void) _setStatusCode: (int)code text: (NSString*)text;
|
||||
- (void) _setValue: (NSString *)value forHTTPHeaderField: (NSString *)field;
|
||||
- (NSString *) _valueForHTTPHeaderField: (NSString *)field;
|
||||
- (NSString*) _valueForHTTPHeaderField: (NSString*)field;
|
||||
@end
|
||||
|
||||
|
||||
|
|
|
@ -74,16 +74,27 @@ typedef struct {
|
|||
|
||||
static NSMutableArray *registered = nil;
|
||||
static NSLock *regLock = nil;
|
||||
static Class abstractClass = nil;
|
||||
static NSURLProtocol *placeholder = nil;
|
||||
|
||||
@implementation NSURLProtocol
|
||||
|
||||
+ (id) allocWithZone: (NSZone*)z
|
||||
{
|
||||
NSURLProtocol *o = [super allocWithZone: z];
|
||||
NSURLProtocol *o;
|
||||
|
||||
if (o != nil)
|
||||
if ((self == abstractClass) && (z == 0 || z == NSDefaultMallocZone()))
|
||||
{
|
||||
o->_NSURLProtocolInternal = NSZoneCalloc(z, 1, sizeof(Internal));
|
||||
/* Return a default placeholder instance to avoid the overhead of
|
||||
* creating and destroying instances of the abstract class.
|
||||
*/
|
||||
o = placeholder;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Create and return an instance of the concrete subclass.
|
||||
*/
|
||||
o = (NSURLProtocol*)NSAllocateObject(self, 0, z);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
@ -92,6 +103,9 @@ static NSLock *regLock = nil;
|
|||
{
|
||||
if (registered == nil)
|
||||
{
|
||||
abstractClass = [NSURLProtocol class];
|
||||
placeholder = (NSURLProtocol*)NSAllocateObject(abstractClass, 0,
|
||||
NSDefaultMallocZone());
|
||||
registered = [NSMutableArray new];
|
||||
regLock = [NSLock new];
|
||||
[self registerClass: [_NSHTTPURLProtocol class]];
|
||||
|
@ -145,6 +159,11 @@ static NSLock *regLock = nil;
|
|||
|
||||
- (void) dealloc
|
||||
{
|
||||
if (self == placeholder)
|
||||
{
|
||||
[self retain];
|
||||
return;
|
||||
}
|
||||
if (this != 0)
|
||||
{
|
||||
[self stopLoading];
|
||||
|
@ -153,6 +172,7 @@ static NSLock *regLock = nil;
|
|||
RELEASE(this->cachedResponse);
|
||||
RELEASE(this->request);
|
||||
NSZoneFree([self zone], this);
|
||||
_NSURLProtocolInternal = 0;
|
||||
}
|
||||
[super dealloc];
|
||||
}
|
||||
|
@ -160,14 +180,27 @@ static NSLock *regLock = nil;
|
|||
- (NSString*) description
|
||||
{
|
||||
return [NSString stringWithFormat:@"%@ %@",
|
||||
[super description], this->request];
|
||||
[super description], this ? (id)this->request : nil];
|
||||
}
|
||||
|
||||
- (id) init
|
||||
{
|
||||
if ((self = [super init]) != nil)
|
||||
{
|
||||
if (isa != abstractClass)
|
||||
{
|
||||
_NSURLProtocolInternal = NSZoneCalloc(GSObjCZone(self),
|
||||
1, sizeof(Internal));
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id) initWithRequest: (NSURLRequest *)request
|
||||
cachedResponse: (NSCachedURLResponse *)cachedResponse
|
||||
client: (id <NSURLProtocolClient>)client
|
||||
{
|
||||
if (isa == [NSURLProtocol class])
|
||||
if (isa == abstractClass)
|
||||
{
|
||||
unsigned count;
|
||||
|
||||
|
@ -189,7 +222,7 @@ static NSLock *regLock = nil;
|
|||
cachedResponse: cachedResponse
|
||||
client: client];
|
||||
}
|
||||
if ((self = [super init]) != nil)
|
||||
if ((self = [self init]) != nil)
|
||||
{
|
||||
this->request = [request copy];
|
||||
this->cachedResponse = RETAIN(cachedResponse);
|
||||
|
@ -390,7 +423,7 @@ static NSLock *regLock = nil;
|
|||
{ // process header line
|
||||
unsigned char *c, *end;
|
||||
NSString *key, *val;
|
||||
#if 0
|
||||
#if 1
|
||||
NSLog(@"process header line len=%d", len);
|
||||
#endif
|
||||
// if it begins with ' ' or '\t' it is a continuation line to the previous header field
|
||||
|
@ -442,6 +475,7 @@ static NSLock *regLock = nil;
|
|||
*/
|
||||
if ([self isKindOfClass: [_NSHTTPSURLProtocol class]])
|
||||
policy=NSURLCacheStorageNotAllowed; // never
|
||||
NSLog(@"Received");
|
||||
[this->client URLProtocol: self didReceiveResponse: response cacheStoragePolicy: policy];
|
||||
}
|
||||
return YES;
|
||||
|
@ -465,7 +499,7 @@ static NSLock *regLock = nil;
|
|||
- (void) _processHeader: (unsigned char *) buffer length: (int) len
|
||||
{ // next header fragment received
|
||||
unsigned char *ptr, *end;
|
||||
#if 0
|
||||
#if 1
|
||||
NSLog(@"received %d bytes", len);
|
||||
#endif
|
||||
if (len <= 0)
|
||||
|
@ -536,17 +570,23 @@ static NSLock *regLock = nil;
|
|||
#endif
|
||||
if (stream == this->input)
|
||||
{
|
||||
#if 1
|
||||
NSLog(@"input stream handleEvent: %x for: %@", event, self);
|
||||
#endif
|
||||
switch(event)
|
||||
{
|
||||
case NSStreamEventHasBytesAvailable:
|
||||
{
|
||||
unsigned char buffer[512];
|
||||
int len=[(NSInputStream *) stream read: buffer maxLength: sizeof(buffer)];
|
||||
int len;
|
||||
|
||||
len = [(NSInputStream *)stream read: buffer
|
||||
maxLength: sizeof(buffer)];
|
||||
if (len < 0)
|
||||
{
|
||||
#if 1
|
||||
#if 1
|
||||
NSLog(@"receive error %@", [NSError _last]);
|
||||
#endif
|
||||
#endif
|
||||
[this->client URLProtocol: self didFailWithError: [NSError errorWithDomain: @"receive error" code: 0 userInfo: nil]];
|
||||
[self _unschedule];
|
||||
return;
|
||||
|
@ -559,9 +599,9 @@ static NSLock *regLock = nil;
|
|||
}
|
||||
case NSStreamEventEndEncountered: // can this occur in parallel to NSStreamEventHasBytesAvailable???
|
||||
{
|
||||
#if 0
|
||||
#if 1
|
||||
NSLog(@"end of response");
|
||||
#endif
|
||||
#endif
|
||||
if (!_readingBody)
|
||||
[this->client URLProtocol: self didFailWithError: [NSError errorWithDomain: @"incomplete header" code: 0 userInfo: nil]];
|
||||
[this->client URLProtocolDidFinishLoading: self];
|
||||
|
@ -571,9 +611,9 @@ static NSLock *regLock = nil;
|
|||
}
|
||||
case NSStreamEventOpenCompleted:
|
||||
{ // prepare to receive header
|
||||
#if 0
|
||||
#if 1
|
||||
NSLog(@"HTTP input stream opened");
|
||||
#endif
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
default:
|
||||
|
@ -583,9 +623,10 @@ static NSLock *regLock = nil;
|
|||
else if (stream == this->output)
|
||||
{
|
||||
unsigned char *msg;
|
||||
#if 0
|
||||
|
||||
#if 0
|
||||
NSLog(@"An event occurred on the output stream.");
|
||||
#endif
|
||||
#endif
|
||||
/* e.g.
|
||||
POST /wiki/Spezial: Search HTTP/1.1
|
||||
Host: de.wikipedia.org
|
||||
|
@ -594,22 +635,24 @@ static NSLock *regLock = nil;
|
|||
|
||||
search=Katzen&go=Artikel <- body
|
||||
*/
|
||||
|
||||
switch(event)
|
||||
{
|
||||
case NSStreamEventOpenCompleted:
|
||||
{
|
||||
#if 0
|
||||
#if 0
|
||||
NSLog(@"HTTP output stream opened");
|
||||
#endif
|
||||
msg=(unsigned char *) [[NSString stringWithFormat: @"%@ %@ HTTP/1.1\r\n",
|
||||
#endif
|
||||
msg = (unsigned char *)[[NSString stringWithFormat:
|
||||
@"%@ %@ HTTP/1.1\r\n",
|
||||
[this->request HTTPMethod],
|
||||
[[this->request URL] absoluteString]
|
||||
] cString]; // FIXME: UTF8???
|
||||
[(NSOutputStream *) stream write: msg maxLength: strlen((char *) msg)];
|
||||
#if 1
|
||||
[[this->request URL] absoluteString]] UTF8String];
|
||||
[(NSOutputStream *) stream write: msg
|
||||
maxLength: strlen((char *) msg)];
|
||||
#if 0
|
||||
NSLog(@"sent %s", msg);
|
||||
#endif
|
||||
_headerEnumerator=[[[this->request allHTTPHeaderFields] objectEnumerator] retain];
|
||||
#endif
|
||||
_headerEnumerator = [[[this->request allHTTPHeaderFields] keyEnumerator] retain];
|
||||
return;
|
||||
}
|
||||
case NSStreamEventHasSpaceAvailable:
|
||||
|
@ -618,12 +661,12 @@ static NSLock *regLock = nil;
|
|||
if (_headerEnumerator)
|
||||
{ // send next header
|
||||
NSString *key;
|
||||
key=[_headerEnumerator nextObject];
|
||||
key = [_headerEnumerator nextObject];
|
||||
if (key)
|
||||
{
|
||||
#if 1
|
||||
#if 0
|
||||
NSLog(@"sending %@: %@", key, [this->request valueForHTTPHeaderField: key]);
|
||||
#endif
|
||||
#endif
|
||||
msg=(unsigned char *)[[NSString stringWithFormat: @"%@: %@\r\n", key, [this->request valueForHTTPHeaderField: key]] UTF8String];
|
||||
}
|
||||
else
|
||||
|
@ -637,9 +680,9 @@ static NSLock *regLock = nil;
|
|||
[_body open];
|
||||
}
|
||||
[(NSOutputStream *) stream write: msg maxLength: strlen((char *) msg)]; // NOTE: we might block here if header value is too long
|
||||
#if 1
|
||||
#if 0
|
||||
NSLog(@"sent %s", msg);
|
||||
#endif
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
else if (_body)
|
||||
|
@ -650,9 +693,9 @@ static NSLock *regLock = nil;
|
|||
int len=[_body read: buffer maxLength: sizeof(buffer)]; // read next block from stream
|
||||
if (len < 0)
|
||||
{
|
||||
#if 1
|
||||
#if 1
|
||||
NSLog(@"error reading from HTTPBody stream %@", [NSError _last]);
|
||||
#endif
|
||||
#endif
|
||||
[self _unschedule];
|
||||
return;
|
||||
}
|
||||
|
@ -660,9 +703,9 @@ static NSLock *regLock = nil;
|
|||
}
|
||||
else
|
||||
{ // done
|
||||
#if 0
|
||||
#if 0
|
||||
NSLog(@"request sent");
|
||||
#endif
|
||||
#endif
|
||||
[self _unschedule]; // well, we should just unschedule the send stream
|
||||
[_body close];
|
||||
[_body release];
|
||||
|
|
|
@ -399,6 +399,5 @@ static const NSMapTableKeyCallBacks headerKeyCallBacks =
|
|||
{
|
||||
return this->statusCode;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
|
|
@ -1268,7 +1268,7 @@ static void setNonblocking(int fd)
|
|||
inputStream: (NSInputStream **)inputStream
|
||||
outputStream: (NSOutputStream **)outputStream
|
||||
{
|
||||
NSString *address = [host address];
|
||||
NSString *address = host ? (id)[host address] : (id)@"127.0.0.1";
|
||||
GSSocketInputStream *ins = nil;
|
||||
GSSocketOutputStream *outs = nil;
|
||||
int sock;
|
||||
|
@ -1281,10 +1281,10 @@ static void setNonblocking(int fd)
|
|||
if (!ins)
|
||||
{
|
||||
#if defined(PF_INET6)
|
||||
ins = [[GSInet6InputStream alloc] initToAddr: address
|
||||
port: port];
|
||||
outs = [[GSInet6OutputStream alloc] initToAddr: address
|
||||
port: port];
|
||||
ins = AUTORELEASE([[GSInet6InputStream alloc]
|
||||
initToAddr: address port: port]);
|
||||
outs = AUTORELEASE([[GSInet6OutputStream alloc]
|
||||
initToAddr: address port: port]);
|
||||
sock = socket(PF_INET6, SOCK_STREAM, 0);
|
||||
#else
|
||||
sock = -1;
|
||||
|
@ -1308,7 +1308,6 @@ static void setNonblocking(int fd)
|
|||
[outs setSibling: ins];
|
||||
*outputStream = outs;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
+ (void) getLocalStreamsToPath: (NSString *)path
|
||||
|
|
|
@ -1926,7 +1926,7 @@ static id propertyForInet6Stream(int descriptor, NSString *key)
|
|||
inputStream: (NSInputStream **)inputStream
|
||||
outputStream: (NSOutputStream **)outputStream
|
||||
{
|
||||
NSString *address = [host address];
|
||||
NSString *address = host ? (id)[host address] : (id)@"127.0.0.1";
|
||||
GSSocketInputStream *ins = nil;
|
||||
GSSocketOutputStream *outs = nil;
|
||||
int sock;
|
||||
|
|
Loading…
Reference in a new issue