解读(上)的总结
上篇解读主要讲解了server服务器端的几个流程,不要担心我们的simpleclient复杂程度,其远远比不上server,只要把server读懂那个就是piece of cake。
代码分析
- accept_request()函数分析
/**********************************************************************/
/* A request has caused a call to accept() on the server port to
* return. Process the request appropriately.
* Parameters: the socket connected to the client */
/**********************************************************************/
void accept_request(void *arg)
{
int client = (intptr_t)arg;
char buf[1024];
size_t numchars;
char method[255];
char url[255];
char path[512];
size_t i, j;
struct stat st;
int cgi = 0; /* becomes true if server decides this is a CGI
* program */
char *query_string = NULL;
numchars = get_line(client, buf, sizeof(buf));
i = 0; j = 0;
while (!ISspace(buf[i]) && (i < sizeof(method) - 1))
{
method[i] = buf[i];
i++;
}
j=i;
method[i] = '\0';
if (strcasecmp(method, "GET") && strcasecmp(method, "POST"))
{
unimplemented(client);
return;
}
if (strcasecmp(method, "POST") == 0)
cgi = 1;
i = 0;
while (ISspace(buf[j]) && (j < numchars))
j++;
while (!ISspace(buf[j]) && (i < sizeof(url) - 1) && (j < numchars))
{
url[i] = buf[j];
i++; j++;
}
url[i] = '\0';
if (strcasecmp(method, "GET") == 0)
{
query_string = url;
while ((*query_string != '?') &&

本文详细解读了TinyHttp服务器端accept_request函数,涉及GET和POST请求的处理,包括URL解析、文件路径查找、CGI判断及HTTP响应生成。通过一步步剖析,展示了如何应对基本的HTTP请求并管理文件资源。
最低0.47元/天 解锁文章
1350

被折叠的 条评论
为什么被折叠?



