Saturday 17 November 2012

First time HTTP servers

There are two things you will need to watch out for if you are to write your own small HTTP server for the first time:
  • The request ends with a line containing exactly
    \r\n
    If you go on and read again, the read will block because nothing new will come. This can be conveniently checked using a BufferedReader.
  • The minimal response you will need to send is
    output.print("HTTP/1.1 200 OK\r\n");
    output.print("\r\n");
    output.print(response);
    output.flush();
    output.close();
    
    A PrintWriter is a good friend here. The "OK" after the response code is not required, but should be written out, since HTTP 1.1 RFC tells us so:
    Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
    

No comments:

Post a Comment