0

When I'm running the server, it works fine on GET / HTTP/1.1 and GET /hello.html HTTP/1.1, but on every other file it tries to access, it appends 'ico' to the path name, what is going wrong?

Thank you!

const char* lookup(const char* path)
{
    // TODO

    char* fileEx = strrchr(path, '.');

    if (fileEx != NULL) {
        if(strcasecmp(fileEx, ".css") == 0) {
            return "text/css";
        }
        else if (strcasecmp(fileEx, ".html") == 0) {
            return "text/html";
        }
        else if (strcasecmp(fileEx, ".gif") == 0) {
            return "image/gif";
        }
        else if (strcasecmp(fileEx, ".ico") == 0) {
            return "image/x-icon";
        }
        else if (strcasecmp(fileEx, ".jpg") == 0) {
            return "image/jpeg";
        }
        else if (strcasecmp(fileEx, ".js") == 0) {
            return "text/javascript";
        }
        else if (strcasecmp(fileEx, ".php") == 0) {
            return "text/x-php";
        }
        else if (strcasecmp(fileEx, ".png") == 0) {
            return "image/png";
        }

    }
    return NULL;
}
2
  • . abs_path not null terminated. Mar 28 '16 at 14:19
  • I'm sorry, but could you explain this in a bit more detail? Thanks a lot!
    – maxrimmer
    Mar 28 '16 at 14:28
1

You abs_path is not null terminated (according to your parse function here). Since the browser is always looking for favicon.ico, abs_path is set to "/favicon.ico". Now your parse comes along and changes abs_path to, lets say, /cat.jpg. If you do not add the null terminator at index 8, then the contents of abs_path will be /cat.jpgn.ico. More specifically, abs_path will always include the "tail" of the name of the longest file you access, because you haven't told it where it should end.

2
  • Of course! I feel stupid now for not thinking about this earlier, thank you so much once again!
    – maxrimmer
    Mar 28 '16 at 15:07
  • No probs. That's why I tried to give you the subtle hint :) Mar 28 '16 at 15:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .