如何格式化HTTP响应 [英] How to format the HTTP response

查看:82
本文介绍了如何格式化HTTP响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C编写了一个套接字程序.我将此程序用作使用TCP的聊天服务器/客户端.我尝试通过将端口更改为80来更改聊天服务器以将其用作HTTP服务器.我在

I have written a socket program in C. I used this program as a chat server/client using TCP. I tried to change the chat server to use it as a HTTP server by changing the port to 80. I referred to the HTTP request/response format in http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Example_session , and made my program to reply with sample response. I tried the url

http://127.0.0.1/ 

在浏览器中

.我的程序读取了请求并回复了响应.起初,我使用google-chrome.直到我在Content-Length标头中添加了正确的数据长度,Chrome才能正确加载页面.设置内容长度标题后,chrome正确加载了页面.但是,firefox不会加载页面. Firefox没有显示任何错误,但是仍在加载页面,就像它仍在等待一些数据一样.仅当我停止服务器或关闭套接字时,才会加载完整页面.我试图按照rfc2616 http://tools.ietf.org/html/rfc2616 进行操作响应完全相同,但结果仍然相同.

in browser. My program read the request and replied with response. At first, I used google-chrome. Chrome didn't load the page correctly until i added the correct data length in Content-Length header. After setting content length header, chrome loaded the page correctly. But, firefox doesn't load the page. Firefox doesn't showed any errors, but still loading the page like it is still waiting for some data. Only When i stop the server or close the socket, complete page is loaded. I tried to follow rfc2616 http://tools.ietf.org/html/rfc2616 , and made the response exactly , but the still the results are same.

请求:

GET/HTTP/1.1 \ r \ n主机:127.0.0.1:8080\r\n用户代理:Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:33.0)Gecko/20100101 Firefox/33.0 \ r \ n接受: text/html,application/xhtml + xml,application/xml; q = 0.9,/; q = 0.8 \ r \ n接受语言: zh-cn,en; q = 0.5 \ r \ n接受编码:gzip,deflate \ r \ n连接: 保持活动状态\ r \ n \ r \ n

GET / HTTP/1.1\r\nHost: 127.0.0.1:8080\r\nUser-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:33.0) Gecko/20100101 Firefox/33.0\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8\r\nAccept-Language: en-US,en;q=0.5\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive\r\n\r\n

对于上述请求,我的程序通过以下响应&写入套接字.内容.

For the above request, my program write to the socket with following response & content.

响应:

HTTP/1.1 200 OK \ r \ nCache-Control:无缓存,私有\ r \ nContent-Length :107 \ r \ n日期:2014年11月24日星期一10:21:21 GMT \ r \ n \ r \ n

HTTP/1.1 200 OK\r\nCache-Control : no-cache, private\r\nContent-Length : 107\r\nDate : Mon, 24 Nov 2014 10:21:21 GMT\r\n\r\n

内容:

<html><head><title></title></head><body>TIME : 1416824843 <br>DATE: Mon Nov 24 15:57:23 2014 </body></html>

此响应正在Chrome中加载,但不会在Firefox中加载. Chrome正在立即加载页面,而Firefox正在等待数据.注意,在标题中指定了数据长度107.我没有在Firefox中启用任何插件.我的Firefox版本在请求中. Chrome版本:38.0.2125.111版(64位).

This response is loading in Chrome, but not in firefox. Chrome is loading the page instantly whereas firefox is waiting for data. Note that the data length 107 is specified in the header. I donot have any addons enabled in firefox. My firefox version is in the request. Chrome version: Version 38.0.2125.111 (64-bit).

代码:

void *socket_read(void *args)
{
    int socket,*s,length;
    char buf[1024];
    s=(int *)args;
    socket=*s;
    while(1){
        buf[0]='\0';
        length=read(socket,buf,1024);
        if(length==0)
            break;
        else if(length==-1){
            perror("Read");
            return;
        }
        else{
            printf("Request: %s\n",buf);
            send_response(socket);
        }       
    }
    printf("End of read thread [%d]\n",socket);
}
int start_accept(int port)
{
    int socket,csocket;
    pthread_t thread;
    struct sockaddr_in client;
    socklen_t addrlen=sizeof(client);
    pthread_attr_t attr;
    socket=create_socket(port);
    while(1){
        if((csocket=accept(socket,(struct sockaddr *)&client,&addrlen))==-1)
        {   
            perror("Accept");
            break;
        }
        pthread_attr_init(&attr);
        if(0!=pthread_create(&thread,&attr,socket_read,&csocket))
        {
            perror("Read thread");
            return;
        }
        usleep(10000);
    }
}
void send_response(int socket)
{
    char buf1[1024];
    int content_length;
    char buf2[1024]="<html><head><title></title></head><body>TIME : 1416824843 <br>DATE: Mon Nov 24 15:57:23 2014 </body></html>";
    content_length=strlen(buf2);
    sprintf(buf1,"HTTP/1.1 200 OK\r\nCache-Control : no-cache, private\r\nContent-Length : %d\r\nDate : Mon, 24 Nov 2014 12:03:43 GMT\r\n\r\n",content_length);
    printf("Written: %d \n",write(socket,buf1,strlen(buf1)));
    fflush(stdout);
    printf("Written: %d \n",write(socket,buf2,content_length));
    fflush(stdout);
}

推荐答案

我发现了问题. 响应不正确.标头字段名称和冒号(':')之间不应有任何空格.在 http://www.w3.org/Protocols/中找到了rfc2616/rfc2616-sec4.html#sec4.2 .

I have found the problem. The Response is incorrect. There should not be any spaces between the header field name and colon(':'). Found this in http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2 .

我的正确回答是

HTTP/1.1 200 OK\r\nCache-Control: no-cache, private\r\nContent-Length: 107\r\nDate: Mon, 24 Nov 2014 10:21:21 GMT\r\n\r\n

我在'Content-Length'和':'之间放置了一个空格.这就是Firefox忽略内容长度标头并读取套接字的原因. Chrome接受标头字段带有空格,因此在chrome中不会发生此问题.

I have put a space between 'Content-Length' and ':' . That's the reason Firefox ignored the content length header and reading the socket. Chrome accepted the header fields with spaces, so the problem doesn't occurred in chrome.

删除空格后,程序运行正常.

After removing the space, program works fine.

这篇关于如何格式化HTTP响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆