0

So, i have looked everywhere for the answer to why my program returns error 505 for cat.exe and non-existant file. I have tested isolating the two functions but they work fine outside of the main program. I know that in the flow of the program it should return 501 after going through lookup but it doesn't and any help would be much appreciated. I have left the code to my parse and lookup below.

'const char* lookup(const char* path) {
char* ptr = strrchr(path,'.');

if (ptr != NULL)
{
    char c = ptr[1];
    int size = 1;
    while(isalpha(c))
    {
         size++;
         c = ptr[size];
    }
    char extension[size + 1];
    for(int i = 0; i < size; i++)
    {
         extension[i] = ptr[i];
    }
    extension[size + 1] = '\0';

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

return NULL;

}`

bool parse(const char* line, char* abs_path, char* query)

{

char a = line[0]; int size = 0;

while(a != ' ')
{
    size++;
    a = line[size];
}    
char* method = malloc(sizeof(char) * size + 1);
for(int i = 0; i < size; i++)
{
    method[i] = line[i];
}
method[size] = '\0';
// If the method is not GET then give 405 error
if (strcmp(method,"GET") != 0)
{
    error(405);
    return false;
}
// If the proceeding char is not a space then give 400 error
if(line[size] != ' ')
{
    error(400);
    return false;
}

int index = 0;
char b = line[4];
while (b != ' ')
{
    index++;
    b = line[4 + index];
}
// Create exact sized storage for the target
char* copy_abspath = malloc(sizeof(char) * index + 1);
// Copy the values from buffer into copy_abspath
for(int i = 0; i < index; i++)
{
    copy_abspath[i] = line[size + 1 + i];
}
copy_abspath[index] = '\0';
// If the first char is not / then give 501 error
if (copy_abspath[0] != '/')
{
    error(501);
    return false;
}

// Iterate over the entire request target and if " is found give 400 error 
for(int i = 0; i < index; i++)
{
    if (copy_abspath[i] == '\"')
    {
        error(400);
        return false;
    }
}
strcpy(abs_path, copy_abspath);
int pos = 0;
// Copy the query into the query address
for(int i = 0; i < index; i++)
{
    if(copy_abspath[i] == '?' && copy_abspath[i + 1] == ' ')
    {
        query[pos] = '\0';
        break;
    }
    else if(copy_abspath[i + 1] != '?' && copy_abspath[i + 1] != ' ')
    {
        int j = i + 1;
        while(copy_abspath[j] != '?' && j < index)
        {
            query[pos] = copy_abspath[j];
            pos++;
            j++;
        }
    }
}
int HTTP_length = 9;
// Array to store the HTTP version request
char* HTTP_version = malloc(sizeof(char) * HTTP_length);
// Copy the HTTP version request from line
for(int i = 0; i < HTTP_length; i++)
{
    HTTP_version[i] = line[index + size + 2 + i];
} 
HTTP_version[HTTP_length] = '\0';
// If the version is not HTTP/1.1 give 505 error
if (strcmp(HTTP_version, "HTTP/1.1") != 0)
{
    error(505);
    return false;
}
/*if(line[index + size + 2 + HTTP_length + 2] != 'r' ||
   line[index + size + 2 + HTTP_length + 4] != 'n')
{
    error(400);
    return false;
}*/

return true;

}

1

server should return 505 in exactly one place, from the parse function http version check. That means that strcmp(HTTP_version, "HTTP/1.1") does not return 0. That means that HTTP_version is not "HTTP/1.1". You could use gdb to determine why. At the least, you could add a printf before the test to see what the value of HTTP_version is. A suggestion is using something like "%s<-\n" as the print format, because non-printing characters (like '\r') can escape the eyes' notice; the "<-" will show you where the string ends.

Or you can hover here to see more specifics on what is causing this problem. It is, of course, up to you to fix it :)

HTTP_version is allocated for 9 bytes, which is indexes 0 through 8, presumably index 8 reserved for the null terminator. The for loop processes 9 times, i = 0 through 8. The null byte goes into index 9. That memory doesn't belong to HTTP_version (memory leak).

| improve this answer | |
  • THANK YOU SO MUCH it used your advice and it now works. It took me so long to debug this program but you just saved me alot of time. :) – Maninder.S Jul 18 '16 at 19:29

You must log in to answer this question.

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