0

I am stuck at pset6. The first check50 is all green which indicates that my lookup and parse functions is correct.

At the second check50 I have the first 13 green but the last 4 is red. That seems to indicate that only my indexes function is wrong. My webserver also works fine. Any help would be much appreciated.

Here are my codes: Indexes function

   char* indexes(const char* path)
{
// First create a new variable called newpath that gets a / appended to the end if that is missing in path.
char pathend = path[strlen(path)-1];
char* end_pointer = "/";
char* newpath = malloc(strlen(path)+1);
strcpy(newpath,path);
if (pathend != '/')
{
   newpath = strcat(newpath,end_pointer);
}


DIR* dir;
dir = opendir(newpath);
if (dir == NULL)
{
    return("1");
}


// creating a list of files and directories in the directory
struct dirent** namelist = NULL;
int n = scandir(newpath, &namelist, NULL, alphasort); 
char* returned_string = malloc(1);
for (int i = 0; i < n; i++)
{
    // iterate over directory entries looking for index.php
    if (strcmp(namelist[i]->d_name, "index.php") == 0)
    {
        const char* strphp ="index.php";
        returned_string = realloc(returned_string,strlen(newpath) + strlen(strphp)+1 );
        returned_string = strcat(newpath, strphp);
        free(newpath);
        //printf("%s", returned_string);
        return(returned_string);
    }
            // iterate over directory entries looking for index.html
    if (strcmp(namelist[i]->d_name, "index.html") == 0)
    {
        const char* strhtml ="index.html";
        returned_string = realloc(returned_string, strlen(path) + strlen(strhtml)+1 );
        returned_string = strcat(newpath, strhtml);
        free(newpath);
        //printf("%s", returned_string);
        return( returned_string);
    }
}
// if neither index.php or index.html exist in path just return NULL
return NULL;
}

Load function:

  bool load(FILE* file, BYTE** content, size_t* length)
{
if (file == NULL)
{
    return false;
}

char* bytearray = malloc(sizeof(BYTE)*1);
int counter = 0; 
for (int c = fgetc(file); c != EOF; c = fgetc(file))
{
  bytearray = realloc(bytearray, sizeof(BYTE) * counter + 1);
bytearray[counter] = (char) c;
counter = counter + 1;  
}

*content = bytearray;
*length = counter;

return true;
}

Lookup function:

   const char* lookup(const char* path)
 {
const char chardot = '.';
char *address_dot_pointer;
address_dot_pointer = strrchr(path, chardot); // the last . in the path string is now pointed at by adress_dot_pointer

if(address_dot_pointer == NULL)
{
    return "NULL";
}

char* css_pointer = ".css";
char* html_pointer = ".html";
char* gif_pointer = ".gif";
char* ico_pointer = ".ico";
char* jpg_pointer = ".jpg";
char* js_pointer = ".js";
char* php_pointer = ".php";
char* png_pointer = ".png";


if ( strcasecmp(address_dot_pointer, html_pointer) == 0 )
{
return "text/html";
}

else if( strcasecmp(address_dot_pointer, css_pointer) == 0 )
{
return "text/css";
}



else if ( strcasecmp(address_dot_pointer, gif_pointer) == 0 )
{
return "image/gif";
}

else if ( strcasecmp(address_dot_pointer, ico_pointer) == 0 )
{
return "image/x-icon";
}

else if ( strcasecmp(address_dot_pointer, jpg_pointer) == 0 )
{
return "image/jpeg";
}

else if ( strcasecmp(address_dot_pointer, js_pointer) == 0 )
{
return "text/javascript";
}

else if ( strcasecmp(address_dot_pointer, php_pointer) == 0 )
{
return "text/x-php";
}

else if ( strcasecmp(address_dot_pointer, png_pointer) == 0 )
{
return "image/png";
}

else
{
return "NULL";
}
}

Parse function:

bool parse(const char* line, char* abs_path, char* query)
{
    char quote = '\"';
    char* a_questionmark = "?";
    char a_space = ' ';
    const char* lookuptest;
    char method[4];
    //memset(method, '\0', 4); // sets the first 4 characters to the null character in method
    strncpy (method,line,3); // this strncpy command copies up to 3 characters from line to method
    char* get ="GET";

    if(! strcmp(method,get) == 0)  // if method is not GET
    {
    error(405);
    return false;
    }

    if(! (line[3] == a_space))  // if the first character after GET is not a space
    {
    printf("405 Method Not Allowed");
    error(405);
    return false;
    }

    char* backslash = "/";
    const char* start_of_backslash_in_line ;
    start_of_backslash_in_line = strchr(line, '/'); //strchr finds the first occurence of the character '/' in line and returns a pointer


    if(! (line[4] == backslash[0]) ) // line = GET request-target SP HTTP-version CRLF , request-target must start with /
    {

        error(501);
        return false;
    }
    else
    {
        for(int i = 0; i < LimitRequestLine + 1; i++)
        {
            if(start_of_backslash_in_line[i] == quote) // if request-target contains a ", respond to the browser with 400 Bad Request and return false;
            {
                printf(" 400 Bad Request");
                error(400);
                return false;
            }

            if( start_of_backslash_in_line[i] == a_questionmark[0] || start_of_backslash_in_line[i] == a_space ) // a space or a ? means
            {                                                   // that the absolute path has ended
                lookuptest = lookup(abs_path);
                if(strcmp (lookuptest,"NULL") == 0)
                {
                    error(501);
                    return false;
                }
            abs_path[i] = '\0';
            break;
            }
        abs_path[i] =  start_of_backslash_in_line[i];
        }
    }

    char* start_query;
    start_query = strchr(line,a_questionmark[0]); //strchr finds the first occurence of "?" in line and returns a pointer to "?" in line
    if(start_query == '\0') // if there is no questionmark in line, query becomes equal to null. 
    query[0] = '\0';

    else if(start_query[0] == '?')
    {
        if(start_query[1] == ' ')
        {
        query[0] = '\0'; // query must also be null if there is only a '?' after absolute path in request target and nothing else
        }
        else
        {
            for(int k = 1 ; k < LimitRequestLine+1; k++) // starts at k=1 because start_query[0] is a '?'
            {
                if(start_query[k] == a_space)
               {
                   query[k-1] = '\0';
                    break;
                }

                query[k-1] = start_query[k];
            }
        }
    }

    for(int i = 0; i < sizeof(query); i++)
        {
            if(query[i] == quote)
            {
                printf(" 400 Bad Request");
                error(400);
                return false;
            }
        }


    char* start_of_HTTP;
    start_of_HTTP = strchr(start_of_backslash_in_line, a_space); // finds the first occurence of " " in start_of_backslash_in_line
   char* HTTP_TYPE;
    HTTP_TYPE = " HTTP/1.1"; // beware of the space in front of HTTP/1.1 
    char* HTTP_string = malloc(9);
     strncpy ( HTTP_string ,start_of_HTTP,9);
    if( ! strcmp (HTTP_string, HTTP_TYPE) == 0)
    {
        error(505);
        return false;
    }


    return true;  
}

1 Answer 1

1

I've copied and pasted your functions inside my server.c file and run check50 on them one at a time. Here's what I have to say:

lookup(): There’s one error in your lookup() function. You return "NULL” (a string) when you should be returning NULL (without quotes, indicating the NULL value). You have to fix that or your function will fail when the input is cat.exe

load(): Your load() function passes all tests, so the problem isn’t there (despite the fact that I think calling realloc() after every byte is a terrible and expensive design decision)

indexes(): Your indexes() function is broken. Let me give you a tip: learn about the function access() and rewrite indexes() with it. It’ll be way easier to get indexes() right using access()

parse(): Your parse() function is also broken, but I suggest you fix the mistakes I’ve pointed above and then try to rewrite parse() with them in mind.

1
  • 1
    Thank you I only have an error on the very last one now. This can probably be fixed by freeing some variables in my code. The quotes over my NULL in the lookup function I would have never found myself Im afraid. :( Requesting two files in a row (cat.html then cat.jpg) succeeds Great help Commented Oct 4, 2016 at 16:13

You must log in to answer this question.

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