1
0

webc.i 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. struct web_client_http:ev::io
  2. {
  3. char*m_buff;
  4. int m_pos,m_size;
  5. int m_fd,m_status;
  6. web_client_http()
  7. {
  8. m_fd=-1;
  9. m_pos=0;
  10. m_size=4096;
  11. m_buff=(char*)malloc(m_size);
  12. }
  13. int connect_tcp(const char*ip,int port)
  14. {
  15. int fd=zio::build_stream();
  16. if(zio::connect(fd,"127.0.0.1",4000))
  17. {
  18. zio::close(fd);
  19. return m_fd=-1;
  20. }
  21. zio::setiobuf(fd,16<<10,16<<10);
  22. zio::setblocking(fd,false);
  23. return m_fd=fd;
  24. }
  25. int connect_ws(const char*ip,int port)
  26. {
  27. const char*fmt=
  28. "GET / HTTP/1.1\n"
  29. "Connection:Upgrade\n"
  30. "Host:127.0.0.1:8088\n"
  31. "Sec-WebSocket-Extensions:x-webkit-deflate-frame\n"
  32. "Sec-WebSocket-Key:puVOuWb7rel6z2AVZBKnfw==\n"
  33. "Sec-WebSocket-Version:13\n"
  34. "Upgrade:webet\n";
  35. if(connect_tcp(ip,port)<0)
  36. return -1;
  37. int len=strlen(fmt);
  38. if(len!=zio::writev(m_fd,fmt,len))
  39. {
  40. close();
  41. return -1;
  42. }
  43. for(;;)
  44. {
  45. len=zio::read(m_fd,m_buff+m_pos,m_size-m_pos);
  46. if(len==-1)
  47. {
  48. close();
  49. return -1;
  50. }
  51. if(len==-2)
  52. continue;
  53. m_pos+=len;
  54. m_buff[m_pos]=0;
  55. if(strstr(m_buff,"\n\n"))
  56. break;
  57. }
  58. log_info("http debug:%s",m_buff);
  59. }
  60. void close()
  61. {
  62. zio::close(m_fd);
  63. }
  64. ~web_client_http()
  65. {
  66. close();
  67. ::free(m_buff);
  68. }
  69. };