static void on_read_proxy_line(h2o_socket_t *sock, int status) { struct st_h2o_accept_data_t *data = sock->data; if (status != 0) { free_accept_data(data); h2o_socket_close(sock); return; } struct sockaddr_storage addr; socklen_t addrlen; ssize_t r = parse_proxy_line(sock->input->bytes, sock->input->size, (void *)&addr, &addrlen); switch (r) { case -1: /* error, just pass the input to the next handler */ break; case -2: /* incomplete */ return; default: h2o_buffer_consume(&sock->input, r); if (addrlen != 0) h2o_socket_setpeername(sock, (void *)&addr, addrlen); break; } if (data->ctx->ssl_ctx != NULL) { h2o_socket_ssl_server_handshake(sock, data->ctx->ssl_ctx, on_ssl_handshake_complete); } else { h2o_accept_ctx_t *ctx = free_accept_data(data); sock->data = NULL; h2o_http1_accept(ctx, sock); } }
static void on_accept(h2o_socket_t *listener, int status) { struct listener_ctx_t *ctx = listener->data; struct config_t *conf = H2O_STRUCT_FROM_MEMBER(struct config_t, global_config, ctx->ctx->global_config); int num_accepts = 16; if (status == -1) { return; } do { h2o_socket_t *sock; if (conf->state.num_connections >= conf->max_connections) break; if ((sock = h2o_evloop_socket_accept(listener)) == NULL) { break; } __sync_add_and_fetch(&conf->state.num_connections, 1); if (ctx->ssl_ctx != NULL) h2o_accept_ssl(ctx->ctx, sock, ctx->ssl_ctx); else h2o_http1_accept(ctx->ctx, sock); } while (--num_accepts != 0); }
static void on_ssl_handshake_complete(h2o_socket_t *sock, int status) { h2o_accept_ctx_t *ctx = free_accept_data(sock->data); sock->data = NULL; if (status != 0) { h2o_socket_close(sock); return; } h2o_iovec_t proto = h2o_socket_ssl_get_selected_protocol(sock); const h2o_iovec_t *ident; for (ident = h2o_http2_alpn_protocols; ident->len != 0; ++ident) { if (proto.len == ident->len && memcmp(proto.base, ident->base, proto.len) == 0) { goto Is_Http2; } } /* connect as http1 */ h2o_http1_accept(ctx, sock); return; Is_Http2: /* connect as http2 */ h2o_http2_accept(ctx, sock); }
static void on_ssl_handshake_complete(h2o_socket_t *sock, int status) { struct st_h2o_accept_data_t *data = sock->data; sock->data = NULL; if (status != 0) { h2o_socket_close(sock); goto Exit; } h2o_iovec_t proto = h2o_socket_ssl_get_selected_protocol(sock); const h2o_iovec_t *ident; for (ident = h2o_http2_alpn_protocols; ident->len != 0; ++ident) { if (proto.len == ident->len && memcmp(proto.base, ident->base, proto.len) == 0) { /* connect as http2 */ h2o_http2_accept(data->ctx, sock, data->connected_at); goto Exit; } } /* connect as http1 */ h2o_http1_accept(data->ctx, sock, data->connected_at); Exit: free_accept_data(data); }
void h2o_accept(h2o_accept_ctx_t *ctx, h2o_socket_t *sock) { if (ctx->expect_proxy_line || ctx->ssl_ctx != NULL) { create_accept_data(ctx, sock); if (ctx->expect_proxy_line) { h2o_socket_read_start(sock, on_read_proxy_line); } else { h2o_socket_ssl_server_handshake(sock, ctx->ssl_ctx, on_ssl_handshake_complete); } } else { h2o_http1_accept(ctx, sock); } }
static void on_config_port_accept(h2o_socket_t *listener, int status) { h2o_context_t *ctx = listener->data; h2o_socket_t *sock; if (status == -1) { return; } if ((sock = h2o_evloop_socket_accept(listener)) == NULL) { return; } h2o_http1_accept(ctx, sock); }
void h2o_accept(h2o_accept_ctx_t *ctx, h2o_socket_t *sock) { struct timeval connected_at = *h2o_get_timestamp(ctx->ctx, NULL, NULL); if (ctx->expect_proxy_line || ctx->ssl_ctx != NULL) { create_accept_data(ctx, sock, connected_at); if (ctx->expect_proxy_line) { h2o_socket_read_start(sock, on_read_proxy_line); } else { h2o_socket_ssl_server_handshake(sock, ctx->ssl_ctx, on_ssl_handshake_complete); } } else { h2o_http1_accept(ctx, sock, connected_at); } }
static void on_accept(h2o_socket_t *listener, int status) { h2o_socket_t *sock; if (status == -1) { return; } if ((sock = h2o_evloop_socket_accept(listener)) == NULL) { return; } if (ssl_ctx != NULL) h2o_accept_ssl(&ctx, sock, ssl_ctx); else h2o_http1_accept(&ctx, sock); }
/** Received an HTTP connection from client */ static void __on_http_connection(uv_stream_t *listener, const int status) { int e; if (0 != status) uv_fatal(status); uv_tcp_t *tcp = calloc(1, sizeof(*tcp)); e = uv_tcp_init(listener->loop, tcp); if (0 != status) uv_fatal(e); e = uv_accept(listener, (uv_stream_t*)tcp); if (0 != e) uv_fatal(e); h2o_socket_t *sock = h2o_uv_socket_create((uv_stream_t*)tcp, (uv_close_cb)free); h2o_http1_accept(&sv->ctx, sv->cfg.hosts, sock); }
static void on_connect(uv_stream_t *server, int status) { uv_tcp_t *conn; h2o_socket_t *sock; if (status != 0) return; conn = h2o_malloc(sizeof(*conn)); uv_tcp_init(server->loop, conn); if (uv_accept(server, (uv_stream_t*)conn) != 0) { uv_close((uv_handle_t*)conn, (uv_close_cb)free); return; } sock = h2o_uv_socket_create((uv_stream_t*)conn, (uv_close_cb)free); if (ssl_ctx != NULL) h2o_accept_ssl(&ctx, sock, ssl_ctx); else h2o_http1_accept(&ctx, sock); }
static void on_accept(uv_stream_t *listener, int status) { uv_tcp_t *conn; h2o_socket_t *sock; if (status != 0) return; conn = h2o_mem_alloc(sizeof(*conn)); uv_tcp_init(listener->loop, conn); if (uv_accept(listener, (uv_stream_t *)conn) != 0) { uv_close((uv_handle_t *)conn, (uv_close_cb)free); return; } sock = h2o_uv_socket_create((uv_stream_t *)conn, NULL, 0, (uv_close_cb)free); if (ssl_ctx != NULL) h2o_accept_ssl(&ctx, ctx.globalconf->hosts, sock, ssl_ctx); else h2o_http1_accept(&ctx, ctx.globalconf->hosts, sock); }