I implemented Jurgen Sweitzer's Webserver application:
http://www.macdevcenter.com/pub/a/mac/2006/11/14/how-to-write-a-cocoa-web-server.html
and modified it for my own purposes. But there is a bug in the program that processes URL's incorrectly. It seems that any spaces in the URL are interpreted as the end of the URL and so URL's with spaces in them are not handled properly.
eg. http://localhost:50000/www.apple.com/Down Loads/index.html
will not load properly in the webserver.
Looking at the code this is where the function is called that processes URL's:
In SimpleHTTPServer.m
@implementation SimpleHTTPServer
- (id)initWithTCPPortunsigned)po delegateid)dl
{
if( self = [super init] ) {
port = po;
delegate = [dl retain];
connections = [[NSMutableArray alloc] init];
requests = [[NSMutableArray alloc] init];
[self setCurrentRequest:nil];
NSAssert(delegate != nil, @"Please specify a delegate");
NSAssert([delegate respondsToSelectorselector(processURL:connection],
@"Delegate needs to implement 'processURL:connection:'");
NSAssert([delegate respondsToSelectorselector(stopProcessing)],
@"Delegate needs to implement 'stopProcessing'");
RespondsToSelector passes a method to the receiver,
"Returns a Boolean value that indicates whether the receiver implements or inherits a method that can respond to a specified message."
and that method is implemented here in AppController.m:
- (void)processURLNSURL *)path connectionSimpleHTTPConnection *)connection
{
// original code
NSString *urlString = [@"http:/" stringByAppendingString:[path absoluteString]];
NSURL *url = [NSURL URLWithString:urlString];
if( url ) {
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:url]];
} else {
NSString *errorMsg = [NSString stringWithFormat"Error in URL: %@", urlString];
NSLog(@"%@", errorMsg);
[server replyWithStatusCode:400 // Bad Request
message:errorMsg];
}
}
It appears that anything after the path that invokes the webserver, or
http://localhost:50000
is passed as a path parameter to processURL, so
http://www.apple.com/Down Loads/index.html
would be the path passed in our example.
The bug is that the space between Down and Loads in our example causes the path passed to be interpreted as http://www.apple.com/Down and so the page doesn't load.
I'm looking for a bugfix, or advice on how to fix this particular bug.
Thanks in advance.
James
http://www.macdevcenter.com/pub/a/mac/2006/11/14/how-to-write-a-cocoa-web-server.html
and modified it for my own purposes. But there is a bug in the program that processes URL's incorrectly. It seems that any spaces in the URL are interpreted as the end of the URL and so URL's with spaces in them are not handled properly.
eg. http://localhost:50000/www.apple.com/Down Loads/index.html
will not load properly in the webserver.
Looking at the code this is where the function is called that processes URL's:
In SimpleHTTPServer.m
@implementation SimpleHTTPServer
- (id)initWithTCPPortunsigned)po delegateid)dl
{
if( self = [super init] ) {
port = po;
delegate = [dl retain];
connections = [[NSMutableArray alloc] init];
requests = [[NSMutableArray alloc] init];
[self setCurrentRequest:nil];
NSAssert(delegate != nil, @"Please specify a delegate");
NSAssert([delegate respondsToSelectorselector(processURL:connection],
@"Delegate needs to implement 'processURL:connection:'");
NSAssert([delegate respondsToSelectorselector(stopProcessing)],
@"Delegate needs to implement 'stopProcessing'");
RespondsToSelector passes a method to the receiver,
"Returns a Boolean value that indicates whether the receiver implements or inherits a method that can respond to a specified message."
and that method is implemented here in AppController.m:
- (void)processURLNSURL *)path connectionSimpleHTTPConnection *)connection
{
// original code
NSString *urlString = [@"http:/" stringByAppendingString:[path absoluteString]];
NSURL *url = [NSURL URLWithString:urlString];
if( url ) {
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:url]];
} else {
NSString *errorMsg = [NSString stringWithFormat"Error in URL: %@", urlString];
NSLog(@"%@", errorMsg);
[server replyWithStatusCode:400 // Bad Request
message:errorMsg];
}
}
It appears that anything after the path that invokes the webserver, or
http://localhost:50000
is passed as a path parameter to processURL, so
http://www.apple.com/Down Loads/index.html
would be the path passed in our example.
The bug is that the space between Down and Loads in our example causes the path passed to be interpreted as http://www.apple.com/Down and so the page doesn't load.
I'm looking for a bugfix, or advice on how to fix this particular bug.
Thanks in advance.
James