static void accept_connection(nxe_loop* loop, nxweb_http_server_listening_socket* lsock) { //static int next_net_thread_idx=0; //nxweb_accept accept_msg; int client_fd; struct sockaddr_in client_addr; socklen_t client_len=sizeof(client_addr); nxe_unset_timer(loop, NXWEB_TIMER_ACCEPT_RETRY, &lsock->accept_retry_timer); while (!shutdown_in_progress) { client_fd=accept4(lsock->listen_source.fd, (struct sockaddr *)&client_addr, &client_len, SOCK_NONBLOCK); if (client_fd!=-1) { if (/*_nxweb_set_non_block(client_fd) ||*/ _nxweb_setup_client_socket(client_fd)) { _nxweb_close_bad_socket(client_fd); nxweb_log_error("failed to setup client socket"); continue; } int lconf_idx=lsock->idx; nxweb_net_thread_data* tdata=(nxweb_net_thread_data*)((char*)(lsock-lconf_idx)-offsetof(nxweb_net_thread_data, listening_sock)); nxweb_http_server_connection* conn=nxp_alloc(tdata->free_conn_pool); nxweb_http_server_connection_init(conn, tdata, lconf_idx); inet_ntop(AF_INET, &client_addr.sin_addr, conn->remote_addr, sizeof(conn->remote_addr)); nxweb_http_server_connection_connect(conn, loop, client_fd); } else { if (errno!=EAGAIN) { nxweb_log_error("accept() failed %d", errno); // retry accept after timeout nxe_set_timer(loop, NXWEB_TIMER_ACCEPT_RETRY, &lsock->accept_retry_timer); } break; } } }
nxweb_http_server_connection* nxweb_http_server_subrequest_start(nxweb_http_server_connection* parent_conn, void (*on_response_ready)(nxweb_http_server_connection* conn, nxe_data data), nxe_data on_response_ready_data, const char* host, const char* uri) { if (parent_conn->connection_closing) return 0; // do not start subrequests if already closing nxweb_net_thread_data* tdata=_nxweb_net_thread_data; nxe_loop* loop=parent_conn->tdata->loop; nxweb_http_server_connection* conn=nxp_alloc(tdata->free_conn_pool); //nxweb_http_server_connection_init(conn, tdata, lconf_idx); memset(conn, 0, sizeof(nxweb_http_server_connection)); conn->uid=nxweb_generate_unique_id(); conn->connected_time=loop->current_time; conn->secure=parent_conn->secure; conn->tdata=tdata; conn->parent=parent_conn; conn->next=parent_conn->subrequests; parent_conn->subrequests=conn; conn->on_response_ready=on_response_ready; conn->on_response_ready_data=on_response_ready_data; nxd_http_server_proto_subrequest_init(&conn->hsp, tdata->free_conn_nxb_pool); conn->events_sub.super.cls.sub_cls=&nxweb_http_server_connection_events_sub_class; conn->worker_complete.super.cls.sub_cls=&nxweb_http_server_connection_worker_complete_class; memcpy(conn->remote_addr, parent_conn->remote_addr, sizeof(conn->remote_addr)); //nxweb_http_server_connection_connect(conn, loop, client_fd); nxe_subscribe(loop, &conn->hsp.events_pub, &conn->events_sub); if (!host) host=parent_conn->hsp.req.host; nxweb_http_server_proto_subrequest_execute(&conn->hsp, host, uri, &parent_conn->hsp.req); return conn; }
void nxd_http_client_proto_start_request(nxd_http_client_proto* hcp, nxweb_http_request* req) { //nxweb_http_request* resp=&hcp->resp; hcp->nxb=nxp_alloc(hcp->nxb_pool); nxb_init(hcp->nxb, NXWEB_CONN_NXB_SIZE); hcp->req=req; if (!req->nxb) req->nxb=hcp->nxb; if (!req->host) req->host=hcp->host; nxe_loop* loop=hcp->data_in.super.loop; hcp->req_headers_ptr=_nxweb_prepare_client_request_headers(req); //nxweb_log_error("REQ: %s", hcp->req_headers_ptr); if (!hcp->req_body_in.pair && req->content && req->content_length>0) { nxd_obuffer_init(&hcp->ob, req->content, req->content_length); nxe_connect_streams(loop, &hcp->ob.data_out, &hcp->req_body_in); } if (hcp->state==HCP_IDLE) hcp->state=HCP_SENDING_HEADERS; nxe_unset_timer(loop, NXWEB_TIMER_KEEP_ALIVE, &hcp->timer_keep_alive); hcp->req_body_sending_started=0; hcp->receiving_100_continue=0; hcp->request_complete=0; hcp->response_body_complete=0; hcp->queued_error_message.l=0; nxe_istream_set_ready(loop, &hcp->data_out); }
void ssi_buffer_init(ssi_buffer* ssib, nxweb_http_server_connection* conn, nxweb_http_request* req) { memset(ssib, 0, sizeof(ssi_buffer)); ssib->req=req; ssib->nxb=nxp_alloc(conn->hsp.nxb_pool); // allocate separate nxb to not interfere with other filters nxb_init(ssib->nxb, NXWEB_CONN_NXB_SIZE); ssib->data_in.super.cls.os_cls=&ssi_buffer_data_in_class; ssib->data_in.ready=1; }
static nxweb_result nxweb_http_proxy_handler_on_headers(nxweb_http_server_connection* conn, nxweb_http_request* req, nxweb_http_response* resp) { nxweb_log_debug("nxweb_http_proxy_handler_on_headers"); nxweb_http_proxy_request_data* rdata=nxb_alloc_obj(conn->hsp.nxb, sizeof(nxweb_http_proxy_request_data)); nxe_loop* loop=conn->tdata->loop; memset(rdata, 0, sizeof(nxweb_http_proxy_request_data)); rdata->conn=conn; conn->hsp.req_data=rdata; conn->hsp.req_finalize=nxweb_http_proxy_request_finalize; rdata->rbuf=nxp_alloc(conn->tdata->free_rbuf_pool); rdata->timer_backend.super.cls.timer_cls=&timer_backend_class; return start_proxy_request(conn, req, rdata); }