用C调试Web服务器
我在课程中PSET6的用C调试Web服务器,c,debugging,pointers,server,cs50,C,Debugging,Pointers,Server,Cs50,我在课程中PSET6的server.c文件中有一个错误,但不知道如何找到它。我正在尝试使用GDB,但它似乎不是很有用 我希望能得到一些帮助,学习如何更好地调试这种程序,如果你能看看我的代码,看看你是否能从一开始就发现任何问题 我的github是: 我关注的职能是: /** * Loads a file into memory dynamically allocated on heap. * Stores address thereof in *content and length ther
server.c
文件中有一个错误,但不知道如何找到它。我正在尝试使用GDB,但它似乎不是很有用
我希望能得到一些帮助,学习如何更好地调试这种程序,如果你能看看我的代码,看看你是否能从一开始就发现任何问题
我的github是:
我关注的职能是:
/**
* Loads a file into memory dynamically allocated on heap.
* Stores address thereof in *content and length thereof in *length.
*/
bool load(FILE* file, BYTE** content, size_t* length)
{
//get the actual length of the file by finding pointer at the end of file and have it reruend via ftell()
fseek(file, 0, SEEK_END);
*length = ftell(file);
fseek(file, 0, SEEK_SET);
//initialize memory and all for storing the file chars, use calloc to be able to diff. garbage
char* get_chars = calloc(sizeof(char) * (*length), sizeof(char));
if (get_chars == NULL)
return false;
//read in chars if interpreter file to new variable get_chars
fread(get_chars, *length, sizeof(BYTE), file);
//save the chars address into the pointer we want for content
*content = get_chars;
free(get_chars);
fclose(file);
if (*content != NULL)
return true;
else
return false;
}
及
及
/**
*返回受支持扩展的MIME类型,否则为NULL。
*/
最后
/**
* Parses a request-line, storing its absolute-path at abs_path
* and its query string at query, both of which are assumed
* to be at least of length LimitRequestLine + 1.
*/
bool parse(const char* line, char* abs_path, char* query)
{
int length = strlen(line) + 1;
//copy the line into new so we can use strsep and null over chars
char* copyline = malloc(sizeof(char) * length);
strncpy(copyline, line, length);
//find token deliminated by space and repeat with NULL to use same string
char* method = strtok(copyline, " ");
char* request = strtok(NULL, " ");
char* version = strtok(NULL, " ");
//return error 405 if method is not GET, string compare method to find out
if (strcmp(method, "GET") != 0)
{
error(405);
return false;
}
//return error 502 if request-target does not start with /
if (*request != '/')
{
error(501);
return false;
}
//if there is a " in the reuest, return an error
if (strchr("\"", *request) != NULL)
{
error(400);
return false;
}
//HTTP version has to be 1.1
if (strcmp(version, "HTTP/1.1\r\n") != 0)
{
error(505);
return false;
}
//if there is no query, return nothing for query and copy the requst in absoulte path
if (strchr(request, '?') == NULL)
{
strcpy(query, "");
strncpy(abs_path, request, strlen(request));
}
//if there is a query
else
{
//will store before ? as a token and after ? as the second token. see test.c
strcpy(abs_path, strtok(request, "?"));
strcpy(query, strtok(NULL, "?"));
}
//add null-terminator to the end of paths
//abs_path[strlen(abs_path)] = '/0';
//query[strlen(query)] = '/0';
strcat(abs_path, "\0");
strcat(query, "\0");
free(copyline);
return true;
}
您不能在
load
功能中释放(获取字符)
。这将释放您刚才分配的缓冲区,并将*内容
指向无效地址。谢谢,我已进行了编辑。然而,它仍然不起作用。我相信现在是索引函数。
const char* lookup(const char* path)
{
//check if path is given
if (path == NULL)
{
return NULL;
}
//returns pointer of last occurance of '.'
char* extension = strrchr(path,'.');
//compares the ext. to the actual ext. given and returns correct format
if (strcasecmp("css", extension) == 0)
{
return "text/css";
}
else if (strcasecmp("html", extension) == 0)
{
return "text/html";
}
else if (strcasecmp("gif", extension) == 0)
{
return "image/gif";
}
else if (strcasecmp("ico", extension) == 0)
{
return "image/x-icon";
}
else if (strcasecmp("jpg", extension) == 0)
{
return "image/jpeg";
}
else if (strcasecmp("js", extension) == 0)
{
return "text/javascript";
}
else if (strcasecmp("png", extension) == 0)
{
return "image/png";
}
else
return NULL;
}
/**
* Parses a request-line, storing its absolute-path at abs_path
* and its query string at query, both of which are assumed
* to be at least of length LimitRequestLine + 1.
*/
bool parse(const char* line, char* abs_path, char* query)
{
int length = strlen(line) + 1;
//copy the line into new so we can use strsep and null over chars
char* copyline = malloc(sizeof(char) * length);
strncpy(copyline, line, length);
//find token deliminated by space and repeat with NULL to use same string
char* method = strtok(copyline, " ");
char* request = strtok(NULL, " ");
char* version = strtok(NULL, " ");
//return error 405 if method is not GET, string compare method to find out
if (strcmp(method, "GET") != 0)
{
error(405);
return false;
}
//return error 502 if request-target does not start with /
if (*request != '/')
{
error(501);
return false;
}
//if there is a " in the reuest, return an error
if (strchr("\"", *request) != NULL)
{
error(400);
return false;
}
//HTTP version has to be 1.1
if (strcmp(version, "HTTP/1.1\r\n") != 0)
{
error(505);
return false;
}
//if there is no query, return nothing for query and copy the requst in absoulte path
if (strchr(request, '?') == NULL)
{
strcpy(query, "");
strncpy(abs_path, request, strlen(request));
}
//if there is a query
else
{
//will store before ? as a token and after ? as the second token. see test.c
strcpy(abs_path, strtok(request, "?"));
strcpy(query, strtok(NULL, "?"));
}
//add null-terminator to the end of paths
//abs_path[strlen(abs_path)] = '/0';
//query[strlen(query)] = '/0';
strcat(abs_path, "\0");
strcat(query, "\0");
free(copyline);
return true;
}