| 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
| 27 */ |
27 */ |
| 28 |
28 |
| 29 #include "httpclient.h" |
29 #include "httpclient.h" |
| |
30 |
| |
31 #include <cx/buffer.h> |
| |
32 |
| |
33 static int client_connected(EventHandler *ev, Event *event); |
| 30 |
34 |
| 31 HttpClient* http_client_new(EventHandler *ev) { |
35 HttpClient* http_client_new(EventHandler *ev) { |
| 32 CxMempool *mp = cxMempoolCreate(32, CX_MEMPOOL_TYPE_PURE); |
36 CxMempool *mp = cxMempoolCreate(32, CX_MEMPOOL_TYPE_PURE); |
| 33 if(!mp) { |
37 if(!mp) { |
| 34 return NULL; |
38 return NULL; |
| 84 cxFree(client->mp->allocator, n.ptr); |
88 cxFree(client->mp->allocator, n.ptr); |
| 85 cxFree(client->mp->allocator, v.ptr); |
89 cxFree(client->mp->allocator, v.ptr); |
| 86 } |
90 } |
| 87 return err; |
91 return err; |
| 88 } |
92 } |
| |
93 |
| |
94 int http_client_start(HttpClient *client) { |
| |
95 int socketfd = socket(AF_INET, SOCK_STREAM, 0); |
| |
96 if(socketfd < 0) { |
| |
97 return 1; |
| |
98 } |
| |
99 |
| |
100 int flags; |
| |
101 if ((flags = fcntl(socketfd, F_GETFL, 0)) == -1) { |
| |
102 flags = 0; |
| |
103 } |
| |
104 if (fcntl(socketfd, F_SETFL, flags | O_NONBLOCK) != 0) { |
| |
105 close(socketfd); |
| |
106 return 1; |
| |
107 } |
| |
108 |
| |
109 client->writeev.cookie = client; |
| |
110 client->writeev.fn = client_connected; |
| |
111 |
| |
112 int ret = ev_pollout(client->ev, socketfd, &client->writeev); |
| |
113 if(ret) { |
| |
114 close(socketfd); |
| |
115 } |
| |
116 return 1; |
| |
117 } |
| |
118 |
| |
119 static int create_req_buffer(HttpClient *client) { |
| |
120 CxBuffer buf; |
| |
121 if(cxBufferInit(&buf, cxDefaultAllocator, NULL, 1024, CX_BUFFER_AUTO_EXTEND)) { |
| |
122 return 1; |
| |
123 } |
| |
124 |
| |
125 |
| |
126 |
| |
127 return 0; |
| |
128 } |
| |
129 |
| |
130 static int client_connected(EventHandler *ev, Event *event) { |
| |
131 HttpClient *client = event->cookie; |
| |
132 if(create_req_buffer(client)) { |
| |
133 // TODO: set error |
| |
134 return 0; // end |
| |
135 } |
| |
136 |
| |
137 |
| |
138 return 1; |
| |
139 } |
| |
140 |
| |
141 |