My parse
code seems to work fine but after it runs there is a segmentation fault
. It isn't any of the other methods, and I just can't figure out why it is doing this. I think it has something to do with query
and abs_path
though.
Here is my parse
method:
char requestLine[strlen(line) + 1];
strcpy(requestLine,line);
requestLine[LimitRequestLine] = '\0';
char* methodCopy = strtok(requestLine," ");
char* requestCopy = strtok(NULL," ");
char* httpCopy = strtok(NULL,"\r\n");
char method[strlen(methodCopy)+1];
strcpy(method,methodCopy);
char request[strlen(requestCopy)+1];
strcpy(request,requestCopy);
char http[strlen(httpCopy)+1];
strcpy(http,httpCopy);
method[strlen(method)] = '\0';
request[strlen(request)] = '\0';
http[strlen(http)] = '\0';
if (strcmp(method,"GET") != 0) {
error(405);
}
if (strncmp(request,"/",1) != 0) {
error(501);
}
if (strchr(request,'"') != NULL) {
error(400);
}
if (strcmp(http,"HTTP/1.1") != 0) {
error(505);
}
char* absCopy = strtok(request, "?");
char* queryCopy = strtok(NULL,"\0");
if (queryCopy != NULL) {
strcpy(abs_path,absCopy);
strcpy(query,queryCopy);
abs_path[strlen(abs_path)] = '\0';
query[strlen(query)] = '\0';
} else {
strcpy(abs_path,absCopy);
abs_path[strlen(abs_path)] = '\0';
query[0] = '\0';
}
if (strchr(abs_path,'?') != NULL) {
error(400);
}
if (strchr(query,'"') != NULL) {
error(400);
}
return true;
EDIT:
After using debug50
, it seems the problem might be in my indexes
method on line 4 strcpy(php, path);
. That is where debug50
stops after I get the segmentation fault
.
char* php = "";
char* html = "";
strcpy(php, path);
strcat(php, "/index.php");
strcpy(html, path);
strcat(html, "/index.html");
printf("\n%s\n",php);
printf("\n%s\n",html);
if (access(php,R_OK) != -1) {
return php;
} else if (access(html,R_OK) != -1) {
return html;
}
return NULL;