12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- struct web_client_http:ev::io
- {
- char*m_buff;
- int m_pos,m_size;
- int m_fd,m_status;
- web_client_http()
- {
- m_fd=-1;
- m_pos=0;
- m_size=4096;
- m_buff=(char*)malloc(m_size);
- }
- int connect_tcp(const char*ip,int port)
- {
- int fd=zio::build_stream();
- if(zio::connect(fd,"127.0.0.1",4000))
- {
- zio::close(fd);
- return m_fd=-1;
- }
- zio::setiobuf(fd,16<<10,16<<10);
- zio::setblocking(fd,false);
- return m_fd=fd;
- }
- int connect_ws(const char*ip,int port)
- {
- const char*fmt=
- "GET / HTTP/1.1\n"
- "Connection:Upgrade\n"
- "Host:127.0.0.1:8088\n"
- "Sec-WebSocket-Extensions:x-webkit-deflate-frame\n"
- "Sec-WebSocket-Key:puVOuWb7rel6z2AVZBKnfw==\n"
- "Sec-WebSocket-Version:13\n"
- "Upgrade:webet\n";
- if(connect_tcp(ip,port)<0)
- return -1;
- int len=strlen(fmt);
- if(len!=zio::writev(m_fd,fmt,len))
- {
- close();
- return -1;
- }
- for(;;)
- {
- len=zio::read(m_fd,m_buff+m_pos,m_size-m_pos);
- if(len==-1)
- {
- close();
- return -1;
- }
- if(len==-2)
- continue;
- m_pos+=len;
- m_buff[m_pos]=0;
- if(strstr(m_buff,"\n\n"))
- break;
- }
- log_info("http debug:%s",m_buff);
- }
- void close()
- {
- zio::close(m_fd);
- }
- ~web_client_http()
- {
- close();
- ::free(m_buff);
- }
- };
|