|
| 1 | +#include <stdlib.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <unistd.h> |
| 4 | +#include <fcntl.h> |
| 5 | +#include <errno.h> |
| 6 | +#include <sys/select.h> |
| 7 | +#include <assert.h> |
| 8 | + |
| 9 | +#include <sys/types.h> |
| 10 | +#include <sys/socket.h> |
| 11 | +#include <netdb.h> |
| 12 | +#include <string.h> |
| 13 | + |
| 14 | +#include "hyper.h" |
| 15 | + |
| 16 | + |
| 17 | +struct conn_data { |
| 18 | + int fd; |
| 19 | + hyper_waker *read_waker; |
| 20 | + hyper_waker *write_waker; |
| 21 | +}; |
| 22 | + |
| 23 | +static size_t read_cb(void *userdata, hyper_context *ctx, uint8_t *buf, size_t buf_len) { |
| 24 | + struct conn_data *conn = (struct conn_data *)userdata; |
| 25 | + ssize_t ret = read(conn->fd, buf, buf_len); |
| 26 | + |
| 27 | + if (ret < 0) { |
| 28 | + int err = errno; |
| 29 | + if (err == EAGAIN) { |
| 30 | + // would block, register interest |
| 31 | + if (conn->read_waker != NULL) { |
| 32 | + hyper_waker_free(conn->read_waker); |
| 33 | + } |
| 34 | + conn->read_waker = hyper_context_waker(ctx); |
| 35 | + return HYPER_IO_PENDING; |
| 36 | + } else { |
| 37 | + // kaboom |
| 38 | + return HYPER_IO_ERROR; |
| 39 | + } |
| 40 | + } else { |
| 41 | + return ret; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +static size_t write_cb(void *userdata, hyper_context *ctx, const uint8_t *buf, size_t buf_len) { |
| 46 | + struct conn_data *conn = (struct conn_data *)userdata; |
| 47 | + ssize_t ret = write(conn->fd, buf, buf_len); |
| 48 | + |
| 49 | + if (ret < 0) { |
| 50 | + int err = errno; |
| 51 | + if (err == EAGAIN) { |
| 52 | + // would block, register interest |
| 53 | + if (conn->write_waker != NULL) { |
| 54 | + hyper_waker_free(conn->write_waker); |
| 55 | + } |
| 56 | + conn->write_waker = hyper_context_waker(ctx); |
| 57 | + return HYPER_IO_PENDING; |
| 58 | + } else { |
| 59 | + // kaboom |
| 60 | + return HYPER_IO_ERROR; |
| 61 | + } |
| 62 | + } else { |
| 63 | + return ret; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +static void free_conn_data(struct conn_data *conn) { |
| 68 | + if (conn->read_waker) { |
| 69 | + hyper_waker_free(conn->read_waker); |
| 70 | + conn->read_waker = NULL; |
| 71 | + } |
| 72 | + if (conn->write_waker) { |
| 73 | + hyper_waker_free(conn->write_waker); |
| 74 | + conn->write_waker = NULL; |
| 75 | + } |
| 76 | + |
| 77 | + free(conn); |
| 78 | +} |
| 79 | + |
| 80 | +static int connect_to(const char *host, const char *port) { |
| 81 | + struct addrinfo hints; |
| 82 | + memset(&hints, 0, sizeof(struct addrinfo)); |
| 83 | + hints.ai_family = AF_UNSPEC; |
| 84 | + hints.ai_socktype = SOCK_STREAM; |
| 85 | + |
| 86 | + struct addrinfo *result, *rp; |
| 87 | + if (getaddrinfo(host, port, &hints, &result) != 0) { |
| 88 | + printf("dns failed for %s\n", host); |
| 89 | + return -1; |
| 90 | + } |
| 91 | + |
| 92 | + int sfd; |
| 93 | + for (rp = result; rp != NULL; rp = rp->ai_next) { |
| 94 | + sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); |
| 95 | + if (sfd == -1) { |
| 96 | + continue; |
| 97 | + } |
| 98 | + |
| 99 | + if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1) { |
| 100 | + break; |
| 101 | + } else { |
| 102 | + close(sfd); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + freeaddrinfo(result); |
| 107 | + |
| 108 | + // no address succeeded |
| 109 | + if (rp == NULL) { |
| 110 | + printf("connect failed for %s\n", host); |
| 111 | + return -1; |
| 112 | + } |
| 113 | + |
| 114 | + return sfd; |
| 115 | +} |
| 116 | + |
| 117 | +struct upload_body { |
| 118 | + int fd; |
| 119 | + char *buf; |
| 120 | + size_t len; |
| 121 | +}; |
| 122 | + |
| 123 | +static int poll_req_upload(void *userdata, |
| 124 | + hyper_context *ctx, |
| 125 | + hyper_buf **chunk) { |
| 126 | + struct upload_body* upload = userdata; |
| 127 | + |
| 128 | + ssize_t res = read(upload->fd, upload->buf, upload->len); |
| 129 | + if (res < 0) { |
| 130 | + printf("error reading upload file: %d", errno); |
| 131 | + return HYPER_POLL_ERROR; |
| 132 | + } else if (res == 0) { |
| 133 | + // All done! |
| 134 | + *chunk = NULL; |
| 135 | + return HYPER_POLL_READY; |
| 136 | + } else { |
| 137 | + *chunk = hyper_buf_copy(upload->buf, res); |
| 138 | + return HYPER_POLL_READY; |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +static int print_each_header(void *userdata, |
| 143 | + const uint8_t *name, |
| 144 | + size_t name_len, |
| 145 | + const uint8_t *value, |
| 146 | + size_t value_len) { |
| 147 | + printf("%.*s: %.*s\n", (int) name_len, name, (int) value_len, value); |
| 148 | + return HYPER_ITER_CONTINUE; |
| 149 | +} |
| 150 | + |
| 151 | +typedef enum { |
| 152 | + EXAMPLE_NOT_SET = 0, // tasks we don't know about won't have a userdata set |
| 153 | + EXAMPLE_HANDSHAKE, |
| 154 | + EXAMPLE_SEND, |
| 155 | + EXAMPLE_RESP_BODY |
| 156 | +} example_id; |
| 157 | + |
| 158 | +#define STR_ARG(XX) (uint8_t *)XX, strlen(XX) |
| 159 | + |
| 160 | +int main(int argc, char *argv[]) { |
| 161 | + const char *file = argc > 1 ? argv[1] : NULL; |
| 162 | + const char *host = argc > 2 ? argv[2] : "httpbin.org"; |
| 163 | + const char *port = argc > 3 ? argv[3] : "80"; |
| 164 | + const char *path = argc > 4 ? argv[4] : "/post"; |
| 165 | + |
| 166 | + if (!file) { |
| 167 | + printf("Pass a file path as the first argument.\n"); |
| 168 | + return 1; |
| 169 | + } |
| 170 | + |
| 171 | + struct upload_body upload; |
| 172 | + upload.fd = open(file, O_RDONLY); |
| 173 | + |
| 174 | + if (upload.fd < 0) { |
| 175 | + printf("error opening file to upload: %d", errno); |
| 176 | + return 1; |
| 177 | + } |
| 178 | + printf("connecting to port %s on %s...\n", port, host); |
| 179 | + |
| 180 | + int fd = connect_to(host, port); |
| 181 | + if (fd < 0) { |
| 182 | + return 1; |
| 183 | + } |
| 184 | + printf("connected to %s, now upload to %s\n", host, path); |
| 185 | + |
| 186 | + if (fcntl(fd, F_SETFL, O_NONBLOCK) != 0) { |
| 187 | + printf("failed to set socket to non-blocking\n"); |
| 188 | + return 1; |
| 189 | + } |
| 190 | + |
| 191 | + upload.len = 8192; |
| 192 | + upload.buf = malloc(upload.len); |
| 193 | + |
| 194 | + fd_set fds_read; |
| 195 | + fd_set fds_write; |
| 196 | + fd_set fds_excep; |
| 197 | + |
| 198 | + struct conn_data *conn = malloc(sizeof(struct conn_data)); |
| 199 | + |
| 200 | + conn->fd = fd; |
| 201 | + conn->read_waker = NULL; |
| 202 | + conn->write_waker = NULL; |
| 203 | + |
| 204 | + |
| 205 | + // Hookup the IO |
| 206 | + hyper_io *io = hyper_io_new(); |
| 207 | + hyper_io_set_userdata(io, (void *)conn); |
| 208 | + hyper_io_set_read(io, read_cb); |
| 209 | + hyper_io_set_write(io, write_cb); |
| 210 | + |
| 211 | + printf("http handshake ...\n"); |
| 212 | + |
| 213 | + // We need an executor generally to poll futures |
| 214 | + const hyper_executor *exec = hyper_executor_new(); |
| 215 | + |
| 216 | + // Prepare client options |
| 217 | + hyper_clientconn_options *opts = hyper_clientconn_options_new(); |
| 218 | + hyper_clientconn_options_exec(opts, exec); |
| 219 | + |
| 220 | + hyper_task *handshake = hyper_clientconn_handshake(io, opts); |
| 221 | + hyper_task_set_userdata(handshake, (void *)EXAMPLE_HANDSHAKE); |
| 222 | + |
| 223 | + // Let's wait for the handshake to finish... |
| 224 | + hyper_executor_push(exec, handshake); |
| 225 | + |
| 226 | + // This body will get filled in eventually... |
| 227 | + hyper_body *resp_body = NULL; |
| 228 | + |
| 229 | + // The polling state machine! |
| 230 | + while (1) { |
| 231 | + // Poll all ready tasks and act on them... |
| 232 | + while (1) { |
| 233 | + hyper_task *task = hyper_executor_poll(exec); |
| 234 | + if (!task) { |
| 235 | + break; |
| 236 | + } |
| 237 | + hyper_task_return_type task_type = hyper_task_type(task); |
| 238 | + |
| 239 | + switch ((example_id) hyper_task_userdata(task)) { |
| 240 | + case EXAMPLE_HANDSHAKE: |
| 241 | + ; |
| 242 | + if (task_type == HYPER_TASK_ERROR) { |
| 243 | + printf("handshake error!\n"); |
| 244 | + return 1; |
| 245 | + } |
| 246 | + assert(task_type == HYPER_TASK_CLIENTCONN); |
| 247 | + |
| 248 | + printf("preparing http request ...\n"); |
| 249 | + |
| 250 | + hyper_clientconn *client = hyper_task_value(task); |
| 251 | + hyper_task_free(task); |
| 252 | + |
| 253 | + // Prepare the request |
| 254 | + hyper_request *req = hyper_request_new(); |
| 255 | + if (hyper_request_set_method(req, STR_ARG("POST"))) { |
| 256 | + printf("error setting method\n"); |
| 257 | + return 1; |
| 258 | + } |
| 259 | + if (hyper_request_set_uri(req, STR_ARG(path))) { |
| 260 | + printf("error setting uri\n"); |
| 261 | + return 1; |
| 262 | + } |
| 263 | + |
| 264 | + hyper_headers *req_headers = hyper_request_headers(req); |
| 265 | + hyper_headers_set(req_headers, STR_ARG("host"), STR_ARG(host)); |
| 266 | + |
| 267 | + // Prepare the req body |
| 268 | + hyper_body *body = hyper_body_new(); |
| 269 | + hyper_body_set_userdata(body, &upload); |
| 270 | + hyper_body_set_data_func(body, poll_req_upload); |
| 271 | + hyper_request_set_body(req, body); |
| 272 | + |
| 273 | + // Send it! |
| 274 | + hyper_task *send = hyper_clientconn_send(client, req); |
| 275 | + hyper_task_set_userdata(send, (void *)EXAMPLE_SEND); |
| 276 | + printf("sending ...\n"); |
| 277 | + hyper_executor_push(exec, send); |
| 278 | + |
| 279 | + // For this example, no longer need the client |
| 280 | + hyper_clientconn_free(client); |
| 281 | + |
| 282 | + break; |
| 283 | + case EXAMPLE_SEND: |
| 284 | + ; |
| 285 | + if (task_type == HYPER_TASK_ERROR) { |
| 286 | + printf("send error!\n"); |
| 287 | + return 1; |
| 288 | + } |
| 289 | + assert(task_type == HYPER_TASK_RESPONSE); |
| 290 | + |
| 291 | + // Take the results |
| 292 | + hyper_response *resp = hyper_task_value(task); |
| 293 | + hyper_task_free(task); |
| 294 | + |
| 295 | + uint16_t http_status = hyper_response_status(resp); |
| 296 | + |
| 297 | + printf("\nResponse Status: %d\n", http_status); |
| 298 | + |
| 299 | + hyper_headers *headers = hyper_response_headers(resp); |
| 300 | + hyper_headers_foreach(headers, print_each_header, NULL); |
| 301 | + printf("\n"); |
| 302 | + |
| 303 | + resp_body = hyper_response_body(resp); |
| 304 | + |
| 305 | + // Set us up to peel data from the body a chunk at a time |
| 306 | + hyper_task *body_data = hyper_body_data(resp_body); |
| 307 | + hyper_task_set_userdata(body_data, (void *)EXAMPLE_RESP_BODY); |
| 308 | + hyper_executor_push(exec, body_data); |
| 309 | + |
| 310 | + // No longer need the response |
| 311 | + hyper_response_free(resp); |
| 312 | + |
| 313 | + break; |
| 314 | + case EXAMPLE_RESP_BODY: |
| 315 | + ; |
| 316 | + if (task_type == HYPER_TASK_ERROR) { |
| 317 | + printf("body error!\n"); |
| 318 | + return 1; |
| 319 | + } |
| 320 | + |
| 321 | + if (task_type == HYPER_TASK_BUF) { |
| 322 | + hyper_buf *chunk = hyper_task_value(task); |
| 323 | + write(1, hyper_buf_bytes(chunk), hyper_buf_len(chunk)); |
| 324 | + hyper_buf_free(chunk); |
| 325 | + hyper_task_free(task); |
| 326 | + |
| 327 | + hyper_task *body_data = hyper_body_data(resp_body); |
| 328 | + hyper_task_set_userdata(body_data, (void *)EXAMPLE_RESP_BODY); |
| 329 | + hyper_executor_push(exec, body_data); |
| 330 | + |
| 331 | + break; |
| 332 | + } else { |
| 333 | + assert(task_type == HYPER_TASK_EMPTY); |
| 334 | + hyper_task_free(task); |
| 335 | + hyper_body_free(resp_body); |
| 336 | + |
| 337 | + printf("\n -- Done! -- \n"); |
| 338 | + |
| 339 | + // Cleaning up before exiting |
| 340 | + hyper_executor_free(exec); |
| 341 | + free_conn_data(conn); |
| 342 | + free(upload.buf); |
| 343 | + |
| 344 | + return 0; |
| 345 | + } |
| 346 | + case EXAMPLE_NOT_SET: |
| 347 | + // A background task for hyper completed... |
| 348 | + hyper_task_free(task); |
| 349 | + break; |
| 350 | + } |
| 351 | + } |
| 352 | + |
| 353 | + // All futures are pending on IO work, so select on the fds. |
| 354 | + |
| 355 | + FD_ZERO(&fds_read); |
| 356 | + FD_ZERO(&fds_write); |
| 357 | + FD_ZERO(&fds_excep); |
| 358 | + |
| 359 | + if (conn->read_waker) { |
| 360 | + FD_SET(conn->fd, &fds_read); |
| 361 | + } |
| 362 | + if (conn->write_waker) { |
| 363 | + FD_SET(conn->fd, &fds_write); |
| 364 | + } |
| 365 | + |
| 366 | + int sel_ret = select(conn->fd + 1, &fds_read, &fds_write, &fds_excep, NULL); |
| 367 | + |
| 368 | + if (sel_ret < 0) { |
| 369 | + printf("select() error\n"); |
| 370 | + return 1; |
| 371 | + } else { |
| 372 | + if (FD_ISSET(conn->fd, &fds_read)) { |
| 373 | + hyper_waker_wake(conn->read_waker); |
| 374 | + conn->read_waker = NULL; |
| 375 | + } |
| 376 | + if (FD_ISSET(conn->fd, &fds_write)) { |
| 377 | + hyper_waker_wake(conn->write_waker); |
| 378 | + conn->write_waker = NULL; |
| 379 | + } |
| 380 | + } |
| 381 | + |
| 382 | + } |
| 383 | + |
| 384 | + |
| 385 | + return 0; |
| 386 | +} |