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