Find the answer to your Linux question:
Results 1 to 3 of 3
I'm writing a small webserver example and faced with some strange thing - headers in my server's output are not always interpreted as 'headers' across different browsers. I think that ...
  1. #1
    Linux Newbie
    Join Date
    Apr 2010
    Location
    Novosibirsk, Russia
    Posts
    136

    Question [SOLVED] Writing webserver - how to correctly send headers?

    I'm writing a small webserver example and faced with some strange thing - headers in my server's output are not always interpreted as 'headers' across different browsers. I think that I do something wrong, but don't know what it is. Then, my server's work:
    1. When a client connects, it creates a new thread to handle its request. It pass a socket descriptor as is, and only child thread sends data to the socket and do it only once, at the end of his work.
    Code:
    /* this is the output operation */
    char *default_headers = "200 OK HTTP/1.0\ncontent-type:text/html\n\n";
    char outbuf[40960];
    char *databuf = NULL;
    /* ...
    There are malloc() call for 'databuf' and reading data from an html file into it
    ... */
    
    // merge output
    strcat(outbuf, default_headers);
    strcat(outbuf, databuf);
    
    send(client_sock, outbuf, strlen(outbuf), 0);
    and after it, for example, Firefox prints all of it as content on the page and only '200 OK HTTP/0.9' header is seen in LiveHTTPHeaders, but Konqueror works absolutely as I want - it sees headers and data separately. And what's the matter?

  2. #2
    Linux Engineer rcgreen's Avatar
    Join Date
    May 2006
    Location
    the hills
    Posts
    1,114
    After receiving and interpreting a request message, a server responds with an HTTP response message.

    Response = Status-Line ; Section 6.1
    *(( general-header ; Section 4.5
    | response-header ; Section 6.2
    | entity-header ) CRLF) ; Section 7.1
    CRLF
    [ message-body ] ; Section 7.2

    6.1 Status-Line

    The first line of a Response message is the Status-Line, consisting of the protocol version followed by a numeric status code and its associated textual phrase, with each element separated by SP characters. No CR or LF is allowed except in the final CRLF sequence.

    Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
    HTTP/1.1: Response

    It seems to imply that two cr/lf sequences should separate
    the header from the content. I also believe the standard calls
    for cr/lf, and not just newlines.

  3. #3
    Linux Newbie
    Join Date
    Apr 2010
    Location
    Novosibirsk, Russia
    Posts
    136
    Thanks a lot

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...