Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

wilbur07

macrumors newbie
Original poster
Jun 18, 2008
7
0
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)initWithTCPPort:(unsigned)po delegate:(id)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 respondsToSelector:mad:selector(processURL:connection:)],
@"Delegate needs to implement 'processURL:connection:'");
NSAssert([delegate respondsToSelector:mad:selector(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)processURL:(NSURL *)path connection:(SimpleHTTPConnection *)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:mad:"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
 

iShater

macrumors 604
Aug 13, 2002
7,027
470
Chicagoland
I thought spaces were not valid in URLs and the you have to encode them to use the % something escape character for them. Although we might see spaces in our browsers, they do translate them to the proper character.


my 2c.
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
That's correct, anything with a space in it is not a valid URL. According to the NSURL documentation for +URLWithString:

This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.

Try using the NSString method -stringByAddingPercentEscapesUsingEncoding: on the string before you pass it to a method expecting a URL. Not sure what encoding you should use, I guess either UTF8 or ASCII probably.

EDIT: I think that list contains spaces as well, even though it's not listed there.
 

wilbur07

macrumors newbie
Original poster
Jun 18, 2008
7
0
Re: Bug in Cocoa Webserver app

That's correct, anything with a space in it is not a valid URL. According to the NSURL documentation for +URLWithString:



Try using the NSString method -stringByAddingPercentEscapesUsingEncoding: on the string before you pass it to a method expecting a URL. Not sure what encoding you should use, I guess either UTF8 or ASCII probably.

EDIT: I think that list contains spaces as well, even though it's not listed there.

That would be fine except that if you look at the code you will see that there's no place to put the stringByAddingPercentEscapesUsingEncoding where it will make a difference. The processURL method is called by a selector and takes a path and a connection object, the path is what is left in the url after you type http://localhost:50000 to connect to the webserver. This path coughs up random characters if it meets a space, even if you type in %20 for the space characters . . .

I really don't know what to do.
 

mclaughj

macrumors member
May 17, 2008
30
0
HiRez is correct, you need to use the instance method: StringByAddingPercentEscapesUsingEncoding:

As to where you need to call it: You need to call it before the processURL method is called.

Here's an example:

Code:
NSString *myURLString = @"http://google.com/search?q=Hello World"; //Notice the space after hello
NSURL *myURL = [NSURL URLWithString:[myURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[myURLObject processURL:myURL connection:myConnection]; //This calls the method you thought there was a bug in

I hope this helps. The method you're provided in that WebServer application doesn't do any URL validation, that's why you have to pre-encode it. :apple:
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.