Bug in Cocoa Webserver app

Discussion in 'Mac Programming' started by wilbur07, Aug 14, 2008.

  1. wilbur07 macrumors newbie

    Joined:
    Jun 18, 2008
    #1
    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
    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 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
     
  2. iShater macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #2
    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.
     
  3. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #3
    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.
     
  4. wilbur07 thread starter macrumors newbie

    Joined:
    Jun 18, 2008
    #4
    Re: Bug in Cocoa Webserver app

    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.
     
  5. mclaughj macrumors member

    mclaughj

    Joined:
    May 17, 2008
    #5
    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:
     

Share This Page