如何解决HTTP响应出现问题
我的简单小型Web服务器C出现问题。我使用的URL是:
127.0.0.1:2000/eslami
,我希望看到其中写有beneslami
的简单页面。下面是我的GET请求处理器例程
static char*
process_GET_request(char * URL,unsigned int *response_length){
printf("%s(%u) called with URL = %s\n",__FUNCTION__,__LINE__,URL);
char delimiter[2] = "/";
char *token;
token = strtok(URL,delimiter);
printf("%s\n",token);
char *response = calloc(1,1024);
HTML_create(response,token);
char *header = calloc(1,248 + strlen(response));
HTTP_header_create(header);
strcat(header,response);
*response_length = strlen(header);
free(response);
return header;
}
下面是HTML_create函数
static void
HTML_create(char *response,char *token){
strcpy(response,"<html>"
"<h>names</h>"
"<p>");
strcat(response,token);
strcat(response,"</p>");
strcat(response,"</html>");
}
和HTTP_header_create
static void
HTTP_header_create(char *header){
strcpy(header,"HTTP/1.1 200 OK\r\n"); // Status Line
strcat(header,set_current_date_time("date")); // General Headers
strcat(header,"Connection: close\r\n");
strcat(header,"Server: My Personal HTTP Server\r\n"); //Response Headers
strcat(header,"Accept-Ranges: bytes\r\n");
strcat(header,"Content-Type: text/html\r\n"); //Entity Headers
strcat(header,"Content-Length: \r\n");
strcat(header,set_current_date_time("date_time"));
strcat(header,"\r\n");
}
static char*
set_current_date_time(char *option){
time_t t;
time(&t);
char *temp;
char *day;
int day_number;
char *month;
int year;
char *current_time;
const char delimiter[2] = " ";
temp = ctime(&t);
day = strtok(temp,delimiter);
month = strtok(NULL,delimiter);
day_number = atoi(strtok(NULL,delimiter));
current_time = strtok(NULL,delimiter);
year = atoi(strtok(0,delimiter));
char *output = calloc(1,1024);
if(strcmp(option,"date_time")){
sprintf(output,"Last-Modified: %s,%d %s %d %s GMT\r\n",day,day_number,month,year,current_time);
return output;
}
else if(strcmp(option,"date")){
sprintf(output,"Date: %d %s %d\r\n",year);
return output;
}
return NULL;
}
根据上述设置,我在浏览器上只看到一个空白页。我什至捕获了我的localhost接口,并确认我的服务器发送了如下的HTTP响应
将响应发送给客户端:
HTTP/1.1 200 OK
Last-Modified: Fri,25 Sep 2020 19:27:03 GMT
Connection: close
Server: My Personal HTTP Server
Accept-Ranges: bytes
Content-Type: text/html
Content-Length:
Date: 25 Sep 2020
<html><h>names</h><p>eslami</p></html>
有人能找出我上面的代码有什么问题吗?
谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。