加入收藏 | 设为首页 | 会员中心 | 我要投稿 好传媒网 (https://www.haochuanmei.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长资讯 > 外闻 > 正文

从零实现一个Http服务器

发布时间:2019-04-18 06:44:41 所属栏目:外闻 来源:高性能服务器开发
导读:我始终觉得,天生的出身很重要,但后天的努力更加重要,所以如今的很多科班往往不如后天努力的非科班。所以,我们需要重新给专业和专家下一个定义:所谓专业,就是别人不搞你搞,这就是你的专业;你和别人同时搞,你比别人搞的好,就是专家。 说到http协议

这里我们的http协议使用的是12345端口号而不是默认的80端口。如何侦听12345端口,这个是非常基础的知识了,这里就不介绍了。当我们收到数据以后:

  1. 1void HttpSession::OnRead(const std::shared_ptr<TcpConnection>& conn, Buffer* pBuffer, Timestamp receivTime) 
  2.  2{ 
  3.  3    //LOG_INFO << "Recv a http request from " << conn->peerAddress().toIpPort(); 
  4.  4 
  5.  5    string inbuf; 
  6.  6    //先把所有数据都取出来 
  7.  7    inbuf.append(pBuffer->peek(), pBuffer->readableBytes()); 
  8.  8    //因为一个http包头的数据至少rnrn,所以大于4个字符 
  9.  9    //小于等于4个字符,说明数据未收完,退出,等待网络底层接着收取 
  10. 10    if (inbuf.length() <= 4) 
  11. 11        return; 
  12. 12 
  13. 13    //我们收到的GET请求数据包一般格式如下: 
  14. 14    /* 
  15. 15    GET /register.do?p={%22username%22:%20%2213917043329%22,%20%22nickname%22:%20%22balloon%22,%20%22password%22:%20%22123%22} HTTP/1.1rn 
  16. 16    Host: 120.55.94.78:12345rn 
  17. 17    Connection: keep-alivern 
  18. 18    Upgrade-Insecure-Requests: 1rn 
  19. 19    User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36rn 
  20. 20    Accept-Encoding: gzip, deflatern 
  21. 21    Accept-Language: zh-CN, zh; q=0.9, en; q=0.8rn 
  22. 22    rn 
  23. 23     */ 
  24. 24    //检查是否以rnrn结束,如果不是说明包头不完整,退出 
  25. 25    string end = inbuf.substr(inbuf.length() - 4); 
  26. 26    if (end != "rnrn") 
  27. 27        return; 
  28. 28 
  29. 29    //以rn分割每一行 
  30. 30    std::vector<string> lines; 
  31. 31    StringUtil::Split(inbuf, lines, "rn"); 
  32. 32    if (lines.size() < 1 || lines[0].empty()) 
  33. 33    { 
  34. 34        conn->forceClose(); 
  35. 35        return; 
  36. 36    } 
  37. 37 
  38. 38    std::vector<string> chunk; 
  39. 39    StringUtil::Split(lines[0], chunk, " "); 
  40. 40    //chunk中至少有三个字符串:GET+url+HTTP版本号 
  41. 41    if (chunk.size() < 3) 
  42. 42    { 
  43. 43        conn->forceClose(); 
  44. 44        return; 
  45. 45    } 
  46. 46 
  47. 47    LOG_INFO << "url: " << chunk[1] << " from " << conn->peerAddress().toIpPort(); 
  48. 48    //inbuf = /register.do?p={%22username%22:%20%2213917043329%22,%20%22nickname%22:%20%22balloon%22,%20%22password%22:%20%22123%22} 
  49. 49    std::vector<string> part; 
  50. 50    //通过?分割成前后两端,前面是url,后面是参数 
  51. 51    StringUtil::Split(chunk[1], part, "?"); 
  52. 52    //chunk中至少有三个字符串:GET+url+HTTP版本号 
  53. 53    if (part.size() < 2) 
  54. 54    { 
  55. 55        conn->forceClose(); 
  56. 56        return; 
  57. 57    } 
  58. 58 
  59. 59    string url = part[0]; 
  60. 60    string param = part[1].substr(2); 
  61. 61 
  62. 62    if (!Process(conn, url, param)) 
  63. 63    { 
  64. 64        LOG_ERROR << "handle http request error, from:" << conn->peerAddress().toIpPort() << ", request: " << pBuffer->retrieveAllAsString(); 
  65. 65    } 
  66. 66 
  67. 67    //短连接,处理完关闭连接 
  68. 68    conn->forceClose(); 
  69. 69} 

(编辑:好传媒网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读