1

I'm getting two errors with check 50 and am struggling to understand how to go about this.

:( Requesting cat.exe returns error code 501
   \ expected output, but not "HTTP/1.1 505 HTTP Version Not Supported..."
:( Requesting non-existant file returns error code 404
   \ expected output, but not "HTTP/1.1 505 HTTP Version Not Supported..."

For the first one, I have this statement here that should check for that error, why isn't it working?

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

Also, Can anyone explain in pseudocode how to separate the absolute path and query? I was thinking of going to strtok() way, but then that would make the question mark into a NULL and I wasn't sure if I needed to keep the question mark with anything?

Thanks.

Here's my whole code if that helps.

char* copyLine = malloc(sizeof(LimitRequestLine) + 1);
strcpy(copyLine,line);

char* method = malloc(sizeof(char*) + 100);
method = strtok(copyLine, " ");
char* request_target = malloc(sizeof(LimitRequestLine) + 1);
request_target=  strtok(NULL, " ");
char* HTTP_version = malloc(sizeof(LimitRequestLine) + 1);
HTTP_version = strtok(NULL, " ");

if (strcasecmp(method, "GET") != 0) {
    error(405);
    return false;
}
if(request_target[0]!= '/'){
    error(501);
    return false;
}

//request_target cant have quote
if(strstr(request_target,"\"")!=NULL){
    error(400);
    return false;
}

if(strcasecmp(HTTP_version, "HTTP/1.1") != 0) {
    error(505);
    return false;
}
char* copy_abs_path = malloc(sizeof(LimitRequestLine) + 1);
//copy_abs_path = strtok(request_target, "?");
copy_abs_path = request_target;
char* copy_query = malloc(sizeof(LimitRequestLine) + 1);
// copy_query = strtok(NULL, " ");
for (int i = 0, n = strlen(copy_abs_path); i < n; i++){ 
        if (copy_abs_path[i] == '/' && copy_abs_path[i+1] == '?') copy_query[0] = copy_abs_path[i+2];
        if (copy_abs_path[i] == '/' && copy_abs_path[i+1] != '?') copy_query[0] = copy_abs_path[i+1];
        // if (strlen(query)) < 1) query = "";
        if (copy_query[0] == '\0') break;
}

abs_path = copy_abs_path;
query = copy_query;

//throw in errors
// strtok request target to check if query

//https://www.youtube.com/watch?v=d3kd5KbGB48


free(copyLine);
free(method);
free(request_target);
free(HTTP_version);
free(copy_abs_path);
free(copy_query);
return true;    
1
  • 1
    Remember: LimitRequestLine is an int. sizeof(int) is 4. Jul 27 '16 at 21:03
0

For your first question, have you tried using strncmp() to compare the first character of the request substring buffer to "/" instead of comparing char literals? I haven't tried it your way, but that's how I got my server.c to work.

For your second question, you might want to try using strstr() to search for the question mark in the request substring. If it returns NULL then you can copy the whole request substring to abs_path ... otherwise, copy everything before the question mark into abs_path and everything after the question mark into query.

You will not need to keep the question mark around for any reason.

You must log in to answer this question.

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