如何实现一个http服务器

1. HTTP协议

HTTP请求(Request)

  • 请求行
  • 消息报头
  • 请求正文
POST / HTTP/1.1(CRLF)  <-请求行
Host: 127.0.0.1:8887(CRLF)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8(CRLF)
Accept-Language: zh-cn(CRLF)
Connection: keep-alive(CRLF)
Accept-Encoding: gzip, deflate(CRLF)
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) (CRLF)AppleWebKit/601.5.17 (KHTML, like Gecko) Version/9.1 Safari/601.5.17(CRLF)
(CRLF)  <-这个CLRF代表HTTP消息报头结束
user=jeffrey&pwd=1234   <-这个是提交的内容

HTTP响应(Response)

HTTP/1.1 200 OK(CRLF) <-状态行
Server: nginx(CRLF)
Date: Wed, 08 Jun 2016 10:41:36 GMT(CRLF)
Content-Type: text/html; charset=utf-8(CRLF)
Transfer-Encoding: chunked(CRLF)
Connection: keep-alive(CRLF)
Vary: Accept-Encoding(CRLF)
X-Frame-Options: DENY(CRLF)
X-XSS-Protection: 1; mode=block(CRLF)
X-Content-Type-Options: nosniff(CRLF)
ETag: W/"a1c3bab6de0d56048beb8f0eafbb9ea4"(CRLF)
Cache-Control: max-age=0, private, must-revalidate(CRLF)
Set-Cookie: _session_id=....; path=/; HttpOnly(CRLF)
X-Request-Id: 252cf775-a065-4dc6-bbf8-593c6a982940(CRLF)
X-Runtime: 0.697513(CRLF)
Content-Encoding: gzip(CRLF)
(CRLF)
响应体(body)

2. 文件上传

在最初的 http 协议中,没有上传文件方面的功能。 rfc1867 为 http 协议添加了这个功能。

POST / HTTP/1.1(CRLF)  <-请求行
Host: 127.0.0.1:8887(CRLF)
Content-Type:multipart/form-data;boundary=---------------------------7d33a816d302b6(CRLF)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8(CRLF)
Accept-Language: zh-cn(CRLF)
Connection: keep-alive(CRLF)
Accept-Encoding: gzip, deflate(CRLF)
User-Agent: Mozilla/4.0 (compatible; OpenOffice.org)(CRLF)
(CRLF)  <-这个CLRF代表HTTP消息报头结束
-----------------------------7d33a816d302b6
Content-Disposition: form-data; name="file1"; filename="11.o"(CRLF)
Content-Type: application/octet-stream(CRLF)
(CRLF)
xxxxxxx(代表二进制数据)
-----------------------------7d33a816d302b6
Content-Disposition: form-data; name="file2"; filename="11.o"(CRLF)
Content-Type: text/plain(CRLF)
(CRLF)
ADAAASAD(文本数据)
-----------------------------7d33a816d302b6
Content-Disposition: form-data; name="username"
(CRLF)
xujiguag
-----------------------------7d33a816d302b6--(CRLF)
(CRLF) <- 整个POST数据结束

3. web服务器流程

如何实现一个http服务器_第1张图片
web服务器流程

你可能感兴趣的:(如何实现一个http服务器)