0

When I run

check50 2015.fall.pset6.server1 server.c

I get

:) server.c exists

:) server compiles

:) HTTP/1.0 returns error code 505

:) Method of 'abcGET' returns error code 405

:) Method of 'GETabc' returns error code 405

:) request-target without starting '/' returns error code 501

:) request-target of abc/hello.php returns error code 501

:( Requesting cat.exe returns error code 501 \ expected output, not an exit code of 0

:) Requesting non-existant file returns error code 404

:) Requesting request-target with " returns error code 400

:) Two spaces after GET returns error code

:) A space within the request target returns error code

:) Two spaces before HTTP/1.1 returns error code

I tried many times but still not able to figure out why I keep getting this

:( Requesting cat.exe returns error code 501 \ expected output, not an exit code of 0

Here is my lookup function

    /**
    * Returns MIME type for supported extensions, else NULL.
    */
    const char* lookup(const char* path)
    {
        // initialize a pointer
        char* extension = NULL;

       // storing extension of path at extension
       strcpy(extension, strrchr(path, '.'));

      // checking for every extension then returning value based on that
      if (extension != NULL)
      {
         if (strcasecmp(extension, ".css") == 0)
         {
            return "text/css";
         }
         if(strcasecmp(extension, ".html") == 0)
         {
            return "text/html";
         }
         if (strcasecmp(extension, ".gif") == 0)
         {
             return "image/gif";
         }
         if (strcasecmp(extension, ".ico") == 0)
         {
            return "image/x-icon";
         }
         if (strcasecmp(extension, ".jpg") == 0)
         {
             return "image/jpeg";
         }
         if (strcasecmp(extension, ".js") == 0)
         {
            return "text/javascript";
         }
         if (strcasecmp(extension, ".php") == 0)
         {
             return "text/x-php";
         }
         if (strcasecmp(extension, ".png") == 0)
         {
             return "image/png";
         }
     }
        return NULL;
    }

1 Answer 1

1

EDIT (after code post)
Occam's razor assumes that the "fewest assumptions" are correct assumptions. I assumed that server was returning an exit code of 0, when in fact it is check50. I also assumed, as you did, that if check50 passed a test, then that test worked. Ah, live and learn!

Your lookup function seg faults. As far as I can tell, every time. You would be able to detect this by testing it with a browser/curl and not depending on check50.

The problem lies here:

char* extension = NULL;

       // storing extension of path at extension
       strcpy(extension, strrchr(path, '.'));

You have declared a pointer called extension. You have not allocated any memory to it. Then you are trying to stuff several bytes into it. The strcpy seg faults.

Before you go all malloc on it, think about this. You really only need a pointer to the ".extension" part of path. What is strrchr(path,'.')? It is that very thing you need.

Another thing to consider: Is (extension != NULL) true if path is "cat.exe"?

Happy coding!


Occam's razor

Among competing hypotheses, the one with the fewest assumptions should be selected.

would suggest that your lookup function does an exit 0; when it doesn't find one of the listed extensions. It should return NULL; as per the distro code. A repro case duplicated this check50 result exactly. If that is not the case, you will need to post some code, starting with lookup, to enable proper troubleshooting.

2
  • I didn't use exit, however your answer is really helpful because I got to learn about Occam's razor! :) And I updated my post with code. Commented Apr 4, 2016 at 7:59
  • I fixed it finally, as you said, not allocating any memory caused a seg fault, thank you so much. :) Commented Apr 4, 2016 at 22:20

You must log in to answer this question.

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