我是C的新手,我现在已经在这项任务上工作了大约7个小时 - 请不要说我没有尝试过。
我想在C中解析自编网络服务器的路径。假设我打电话
http://localhost:8080/hello/this/is/a/test.html
然后浏览器
GET /hello/this/is/a/test.html HTTP/1.1
我要解析/hello/this/is/a/test.html
,所以“GET”(注意GET后的空格)和/../../..html
之后的第一个空格之间的完整字符串。
到目前为止我尝试了什么:
int main() {
...
char * getPathOfGetRequest(char *);
char *pathname = getPathOfGetRequest(buf);
printf("%s\n\n%s", buf, pathname);
...
}
char * getPathOfGetRequest(char *buf) {
char *startingGet = "GET ";
char buf_cpy[BUFLEN];
memcpy(buf_cpy, buf, sizeof(buf));
char *urlpath = malloc(1000);
char *path = malloc(1000);
urlpath = strstr(buf_cpy, startingGet);
char delimiter[] = " ";
path = strtok(urlpath, delimiter);
path = strtok(NULL, delimiter);
return path;
}
路径名始终只有4个正确的字符,可能会或可能不会填充其他不相关的字符,例如/hell32984cn)/$"§$
。我想这与strlen(startingGet)有关,但我看不出它之间的关系。我的错误在哪里?
答案 0 :(得分:3)
带有评论的问题代码:
char * getPathOfGetRequest(char *buf) {
char *startingGet = "GET ";
char buf_cpy[BUFLEN];
memcpy(buf_cpy, buf, sizeof(buf));
上面的memcpy可能只会将4个字节从buf复制到buf_cpy。 这是因为buf是一个指向char的指针。 sizeof(buf)是指针的大小(可能是:4)。 也许,使用' strlen()'而不是使用' sizeof()&#39 ;.
char *urlpath = malloc(1000);
char *path = malloc(1000);
urlpath = strstr(buf_cpy, startingGet);
也许提问者不清楚为什么urlpath被分配1000字节的内存。在任何情况下,上述赋值都会导致泄漏1000个字节,并且会破坏' urlpath = malloc(1000)'的目的。
上述陈述的实际效果是urlpath = buf_cpy;
,因为strstr()将返回' GET'开头的位置。在buf_copy中。
char delimiter[] = " ";
path = strtok(urlpath, delimiter);
同样,上面的赋值将导致分配给路径的1000个字节泄露,并且违背了' path = malloc(1000)'上方。
path = strtok(NULL, delimiter);
return path;
}
交替编码:
char *getPathOfGetRequest(const char *buf)
{
const char *start = buf;
const char *end;
char *path=NULL;
size_t pathLen;
/* Verify that there is a 'GET ' at the beginning of the string. */
if(strncmp("GET ", start, 4))
{
fprintf(stderr, "Parse error: 'GET ' is missing.\n");
goto CLEANUP;
}
/* Set the start pointer at the first character beyond the 'GET '. */
start += 4;
/* From the start position, set the end pointer to the first white-space character found in the string. */
end=start;
while(*end && !isspace(*end))
++end;
/* Calculate the path length, and allocate sufficient memory for the path plus string termination. */
pathLen = (end - start);
path = malloc(pathLen + 1);
if(NULL == path)
{
fprintf(stderr, "malloc() failed. \n");
goto CLEANUP;
}
/* Copy the path string to the path storage. */
memcpy(path, start, pathLen);
/* Terminate the string. */
path[pathLen] = '\0';
CLEANUP:
/* Return the allocated storage, or NULL in the event of an error, to the caller. */
return(path);
}
最后,如果' strtok()' 必须使用:
char *getPathOfGetRequest(char *buf)
{
char *path = NULL;
if(strtok(buf, " "))
{
path = strtok(NULL, " ");
if(path)
path=strdup(path);
}
return(path);
}