0

Below are my parse() and lookup() functions for server.c pset6. I have gone over and over but maybe I didn't understand what lookup does. Any help is highly welcome and appreciated. My check50 for server2 is all red while that for server1 is red only at the request for non existant file. Hope I am clear enough for good help. Thank you.

const char* lookup(const char* path) { // TODO if(path == NULL) { error(404); }

else
{
    {
        for( int i = 0, n  = strlen(path); i <= n; i++)
        {
            char * ext = 0;
            char c = '.';

            ext = strchr (path, c);
            int k = atoi(ext);

            if(strcmp(".css",ext) == 0)
            k = 0;

            if(strcmp(".html",ext) == 0)
            k = 1;

            if(strcmp(".gif",ext) == 0)
            k = 2;

            if(strcmp(".ico",ext) == 0)
            k = 3;

            if(strcmp(".jpg",ext) == 0)
            k = 4;

            if(strcmp(".jv",ext) == 0)
            k = 5;

            if(strcmp(".png",ext) == 0)
            k = 6;

            switch(k)
            {
            case 0: return "text/css";
            case 1: return "text/html";
            case 2: return "image/gif";
            case 3: return "image/x-icon";
            case 4: return "image/jpeg";
            case 5: return "text/javascript";
            case 6: return "image/png";
            default:  return NULL;
            }

        }
    }
}
return NULL;

}

and here is parse

bool parse(const char* line, char* abs_path, char* query) { // TODO

if(line[0] == 0)
{
    error(404);
}
// declaring delimiters for future call to strtok().
const char * space = " ";
const char * marker = "?";

// generates modifiable string from const char* line.
int l = strlen(line);
char string[l + 1];
for(int i = 0; i <= l; i++)
{
    string[i] = line[i];
    string[l] = '\0';
}

// creating tokens from string for method, target and version.
char * method = strtok(string, space);
char * target = strtok(NULL, space);
char * version = strtok(NULL, space);

// creating copy of target, storing it in duplicate.
int n = strlen(target);

char duplicate[n + 1];
for (int a = 0; a <= n; a++)
{
    duplicate[a] = target[a];
    duplicate[n] = '\0';

    if(duplicate[0] == 0)
{
    error(404);
}

}


// generating abs_path and query from target.

abs_path = strtok(duplicate, marker);
query = strtok(NULL, marker);

// ensure method is "GET"
        if(strcmp(method, "GET") != 0)
        {
            error(405);
        }
// ensures request-target starts with '/' and has no '"'.

        for(int j = 0; j <= n; j++)
        {
            if(target[0] != '/')
            {
            error(501);
            }

            if(target[j] == '"')
            {
                error(400);
            }
            if(!target)
            {
                error(400);
            }
        }

// ensures abs_path has a '.'
            if(strchr(abs_path, '.') == NULL)
            {
            error(501);
            }

// ensures version is "HTTP/1.1".
            if(strncmp(version, "HTTP/1.1", 8) != 0)
            {
                error(505);
            }


error(501);
return false;

}

0

Scope. Pointers. At least one problem with check50/server1 lies here
abs_path = strtok(duplicate, marker);. duplicate is local to the parse function. The strtok returns a pointer (to the local duplicate). Which will be "destroyed" when control returns to main. (query too!)

Consider using debug50 or gdb (the ADDENDUM) to troubleshoot. You may want to set a break in main when it returns from parse and inspect abs_path in main.

Remember this hint from the spec:

Odds are you’ll find functions like strchr, strcpy, strncpy, and/or strstr of help!

There's a bug in lookup. From the man page atoi() does not detect errors. This line int k = atoi(ext); is wrong. ext is going to be string data, that would be an error. So k could be anything. If it is 0 what will happen in the case statment? Use gdb or printfs to help you troubleshoot. And remember this from the spec:

(or any capitalization thereof),

check50/server2 will not pass if load and indexes are not completed (and correct!).

| improve this answer | |
  • Thank you @DinoCoderSaurus. I didn't understand your idea pretty well but I get the scope bit. I will go back and re-crack my mind. Thanks though. It may be necessary to upload my load and indexes maybe as well, let me retry them first. Thanks again. – Salva Tore Dec 24 '16 at 17:22
  • Alright; my indexes and load are here respectively gist.github.com/7432e4874a2b967cd0c58127b65e230b gist.github.com/a3ee4337ab2dbec44d1045714dc365c2 Thanks. Please screen through if you don't mind and see if there's a concept I don't get. I only will need the ideas 'coz I have to crack this problem. – Salva Tore Dec 24 '16 at 17:54
  • Did you make corrections to parse and pass all server1 tests? You should have a perfect check50/server1 before you move on. Then edit the question to add the indexes and load functions so the answer can be edited. "Troubleshooting" in the comments is not the best use of this forum. Hint for pasting the code: after it is pasted, select it then click the '{}' in the format bar and it will be correctly formatted. – DinoCoderSaurus Dec 24 '16 at 19:22
  • Thanks a lot. I did use stdup() to duplicate target. I created new pointers to store addresses of the passed strings and made these to store the addresses in my new pointers which makes them the same address returned to main. However I do pass check50 server1 well except that I don't return 404 for non existant file and instead I return 501. I think this must be ooutside of parse. I don't want to crowd this discussion, makes it look unrealistic! Is it possible my error in server1 is outside parse as I think! – Salva Tore Dec 24 '16 at 21:55
  • Edited the answer. I will not leave remarks on load and indexes in these comments. – DinoCoderSaurus Dec 25 '16 at 2:49

You must log in to answer this question.

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