0

When i run my code i get a error proxy could not be processed request. During check50 2 everything is red with no reason as to why, I am thinking its a seg fault but valgrind didn't give one. Honestly, no idea where the issue is so any help would be greatly appreciated.

Lookup:

const char* lookup(const char* path)
{

    char* ext = strchr(path, '.');

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

Parse:

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

if (strncmp(line, "GET ", 4) != 0) 
{
    error(405);
    return false;
}

if (strncmp(line, "GET /", 5) !=0)
{
    error(501);
    return false;
}
char* request = strchr(line, '/');
char* httptype = strchr(request, ' ');
char buffer[strlen(request)+1];

strcpy(buffer, request);
char* abscopy = strtok(buffer, " ");


if (abscopy[0] != '/')
{
    error(501);
    return false;
}

if (strchr(abscopy, '"') != NULL)
{
    error(400);
    return false;
}

if (strchr(abscopy, ' ') != NULL)
{
    error(400);
    return false;
}

if (strchr(abscopy, '?') != NULL)
{
    char* absbuffer = malloc(sizeof(abscopy+1));
    strcpy(absbuffer, abscopy);
    char* absnew = strtok(absbuffer, "?");
    strcpy(abs_path, absnew);
}

if (strchr(abscopy, '?') == NULL)
{
    strcpy(abs_path, abscopy);
}

if ((strstr(httptype, "  ")) != NULL)
{
    error(400);
    return false;
}
if ((strstr(httptype, "HTTP/1.1")) == NULL)
{
    error(505);
    return false;
}

char q[] = "q=";

if (strstr(abscopy, q) == NULL)
{
    query = "";   
}
else 
{
    query = strstr(abscopy,q);
}

return false;
}

Load:

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

char* buffer = malloc(sizeof(char));
int counter = 0;
BYTE x = 1;

for (int i = fgetc(file); i != EOF; i = fgetc(file))
{
    buffer[counter] = (char) i;
    counter++;

    if (sizeof(buffer) <= (sizeof(x) * counter))
    {
        buffer = realloc(buffer, sizeof(buffer) * 2);
    }

}

*content = buffer;
*length = counter;

return true;
}

Indexes:

char* indexes(const char* path)
{
int x = strlen(path);

char php_index[] = "/index.php";
char html_index[] = "/index.html";

char* php_path = malloc(x + strlen(php_index)+ 1);
char* html_path = malloc(x + strlen(html_index) + 1);

strcpy(php_path, path);
strcpy(html_path, path);

strcat(php_path, php_index);
strcat(html_path, html_index);

if (access(php_path, F_OK) == 0)
{
    return php_path;
}

if (access(html_path, F_OK) == 0)
{
    return html_path;
}
else
{
return NULL;
}
free(php_path);
free(html_path);
}

1 Answer 1

0

load is a significant contributor to the catastrophic check50 failure. Here are some problem areas:

This declaration char* buffer = malloc(sizeof(char)); is going to cause problems. Image data is not string data. Conventional wisdom suggest declaring buffer as a BYTE pointer (BYTE*).

This buffer[counter] = (char) i; is a problem. image data cannot be cast as char data because a char byte only handles the ascii range of 0 to 127, but an image byte can be 0 to 255. Don't cast i.

This

   if (sizeof(buffer) <= (sizeof(x) * counter))
    {
        buffer = realloc(buffer, sizeof(buffer) * 2);
    }

has a major defect. buffer is a pointer. sizeof a pointer is 8. x is an integer. sizeof an integer is 4.

Since x is declared as 1 and is a constant, you were probably expecting this (sizeof(x) * counter) to evaluate to 1 * counter. When put that way, you should see that a multiplier is unnecessary.

Since buffer is (originally) declared as a char*, I presume you meant strlen(buffer). Now that you know you cannot declare it that way, what to do? You know that it needs to be "as big as" counter + 1. Why +1? Because counter starts at 0.

server is a great exercise to help you get familiar with gdb and gdb is a great tool to help you work through server, find some tips here.

I cannot help with the "proxy" problem you mention, there's not enough information. I do know the message is probably not coming from server, since the word "proxy" never appears in server.c.

1
  • Thank you very much dino! you are aweomse! i was able to fix load and now it works great Commented Sep 9, 2016 at 17:00

You must log in to answer this question.

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