0

So I've been struggling with this for days. I think the logic is OK but I'm not sure what in the while loop is causing the problems. Can anyone point me in the right direction?

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

*content = NULL;
*length = 0;

while(!feof(file))
{
    BYTE buffer[BYTES];
    size_t bytes = fread(buffer, BYTES, 1, file);

    *content = realloc(*content, *length + bytes);

    if (*content == NULL)
    {
        *length = 0;
        break;
    }
    memcpy(*content + *length, buffer, bytes);
    *length += bytes;
}
return true;

}

My parse function only seems to work occasionally(in the first check50). Do I need to allocate memory on the heap in the calls to strtok. I can't think of any other reasons why its failing.

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

        char line1[strlen(line) + 1];
        strcpy(line1, line);

        char* method = strtok(line1, " ");
        char* request_target = strtok(NULL, " ");
        char* HTTPversion = strtok(NULL, "/n/r");

        if(strstr(request_target, "?") != NULL)
         {
             char* qpointer = strrchr(request_target, '?');

           if(qpointer != NULL)
           {
              query = qpointer + 1;
           }
           else
           {
               query = "";
            }

             int abspathlength = strlen(request_target) - strlen(query);
             strncpy(abs_path, request_target, abspathlength);
          }
        else
      {
           strcpy(abs_path, request_target);
           query = "";
      }

*** updated load function. I can't see why this wouldn't work.

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

      *content = NULL;
      *length = 0;
      BYTE buffer[BYTES];

      while(fread(&buffer, 1, BYTES, file) == BYTES)
      {

         content = realloc(*content, *length  + BYTES);

         if (content == NULL)
          {
             length = 0; 
             break;
          }
         memcpy(*content + *length, buffer, BYTES);

         length += BYTES;
     }

     return true;
    }

*** updated parse and load functions 2

bool parse(const char* line, char* abs_path, char* query)
{
char line1[strlen(line) + 1];
strcpy(line1, line);

char* method = strtok(line1, " ");
char* request_target = strtok(NULL, " ");
char* HTTPversion = strtok(NULL, " ");


if(strchr(request_target, '?') != NULL)
{

    char* last = strrchr(request_target, '?');

    strncpy(abs_path, request_target, strlen(request_target) -     strlen(last));

    abs_path[strlen(abs_path) - 1] = 0;

    char* query1 = last + 1;

    if(query1 != NULL)
    {
        strcpy(query, query1);
    }
    else
    {
        strcpy(query, "");    
    }

}
else 
{
    strcpy(abs_path, request_target);
    strcpy(query, "");
}

and load

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

*content = NULL;
*length = 0;
BYTE buffer[BYTES];

while(true)
{

    size_t bytes = fread(&buffer, 1, BYTES, file);

    *content = realloc(*content, *length  + bytes);

    if (content == NULL)
    {
        *length = 0; 
        break;
    }
    memcpy(*content + *length, buffer, bytes);
    *length += bytes;

    if(feof(file))
    {
        break;
    }
}

return true;
}

check50 error

ifanmo@ide50:~/workspace/pset6 $ check50 2015.fall.pset6.server1 server.c :) server.c exists :( server compiles \ killed by server :| HTTP/1.0 returns error code 505 \ can't check until a frown turns upside down :| Method of 'abcGET' returns error code 405 \ can't check until a frown turns upside down

0

Conventional wisdom says don't do this while(!feof(file)). (Week 4 -> Shorts -> File I/O). That may or may not cause you problems. But the fread call will.

The function signature of fread is:
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

According to man fread (emphasis added):

On success, fread() and fwrite() return the number of items read or written. This number equals the number of bytes transferred only when size is 1.

You are calling fread with size = BYTES ( = 512).


This is (at least part of) a problem in parse:
int abspathlength = strlen(request_target) - strlen(query);

query is greater than request_target; it comes "later" in line. And don't forget to allocate space for the null terminator, and then null-terminate!


This looks hinky too: char* HTTPversion = strtok(NULL, "/n/r"); I assume you are looking for the end of "version". From the instructions:

method SP request-target SP HTTP-version CRLF.....
.....CRLF represents \r\n

7
  • Thanks, I changed my load function. It now compiles but seg faults when I click on the link to any of the files. Is there anything in the load function that would be causing this? I've included my updated load function above.
    – user11010
    Apr 29 '16 at 6:02
  • Check out the two places you reference the pointer called length, not the contents of said pointer. Apr 29 '16 at 13:35
  • Hi, I'm still getting 'killed by server' on both checks. It compiles and runs the same as the staff solution and don't have any errors showing up on valgrind. I've included my new load() and parse () above (minus the error checking in parse).
    – user11010
    Apr 30 '16 at 9:14
  • Where specifically do you get "killed by server" in each check? Apr 30 '16 at 12:44
  • It's right after server exists in both check 50s. It won't compile for check50 but works in the browser. Could indexes or lookup have any effect on this?
    – user11010
    Apr 30 '16 at 22:02

This site is temporarily in read only mode and not accepting new answers.

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