2013-09-29 00:21:18 +04:00
|
|
|
/* v/http.c
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <uv.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "all.h"
|
2015-06-24 04:28:38 +03:00
|
|
|
#include "vere/vere.h"
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-22 09:55:39 +03:00
|
|
|
#include "h2o.h"
|
|
|
|
|
2017-11-25 00:32:11 +03:00
|
|
|
static const c3_i TCP_BACKLOG = 16;
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
// XX put this on u3_host ?
|
|
|
|
static h2o_globalconf_t fig_u;
|
|
|
|
|
|
|
|
// XX rename and move to vere.h
|
|
|
|
typedef struct _h2htp {
|
|
|
|
uv_tcp_t wax_u; // server stream handler
|
|
|
|
h2o_context_t* ctx_u; // h2o ctx
|
|
|
|
h2o_accept_ctx_t* cep_u; // h2o accept ctx (wat for?)
|
|
|
|
h2o_hostconf_t* hos_u; // h2o host config
|
|
|
|
c3_w sev_l; // server number
|
|
|
|
c3_w coq_l; // next connection number
|
|
|
|
c3_w por_w; // running port
|
|
|
|
c3_o sec; // logically secure
|
|
|
|
c3_o lop; // loopback-only
|
|
|
|
struct _h2con* hon_u; // connection list
|
|
|
|
struct _h2htp* nex_u; // next in list
|
|
|
|
} h2htp;
|
|
|
|
|
|
|
|
typedef struct _h2con {
|
|
|
|
uv_tcp_t wax_u; // client stream handler
|
|
|
|
h2o_conn_t* con_u; // h2o connection
|
|
|
|
h2o_socket_t* sok_u; // h2o connection socket
|
|
|
|
c3_w ipf_w; // client ipv4
|
|
|
|
c3_w coq_l; // connection number
|
|
|
|
c3_w seq_l; // next request number
|
|
|
|
struct _h2htp* htp_u; // server backlink
|
|
|
|
struct _h2req* req_u; // request list
|
|
|
|
struct _h2con* nex_u; // next in server's list
|
|
|
|
} h2con;
|
|
|
|
|
|
|
|
typedef struct _h2req {
|
|
|
|
h2o_req_t* rec_u; // h2o request
|
|
|
|
c3_w seq_l; // sequence within connection
|
|
|
|
struct _h2con* hon_u; // connection backlink
|
|
|
|
struct _h2req* nex_u; // next in connection's list
|
|
|
|
} h2req;
|
|
|
|
|
|
|
|
// XX u3_Host.htp_u
|
|
|
|
static h2htp* sev_u = 0; // global server list
|
2017-11-22 09:55:39 +03:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
static u3_noun _http_pox_to_noun(c3_w sev_l, c3_w coq_l, c3_w seq_l);
|
|
|
|
static void _http_request(h2req* req_u, u3_noun recq);
|
|
|
|
static void _http_request_kill(h2req* req_u);
|
|
|
|
static void _http_req_link(h2con* hon_u, h2req* req_u);
|
|
|
|
static void _http_req_unlink(h2req* req_u);
|
|
|
|
static void _http_conn_link(h2htp* htp_u, h2con* hon_u);
|
|
|
|
static void _http_conn_unlink(h2con* hon_u);
|
|
|
|
|
|
|
|
// XX keep here (or in new http.h?)
|
|
|
|
typedef struct _h2o_ctx_wrap {
|
|
|
|
h2o_context_t cxt_u; // h2o ctx
|
|
|
|
struct _h2htp* htp_u; // server backlink
|
|
|
|
} h2o_ctx_wrap;
|
2017-11-25 00:31:49 +03:00
|
|
|
|
2017-11-25 01:12:09 +03:00
|
|
|
typedef struct _h2hed {
|
|
|
|
struct _h2hed* nex_u;
|
|
|
|
c3_w nam_w;
|
|
|
|
c3_c* nam_c;
|
|
|
|
c3_w val_w;
|
|
|
|
c3_c* val_c;
|
|
|
|
} h2hed;
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_vec_to_meth(): convert h2o_iovec_t to meth
|
2017-11-22 09:55:39 +03:00
|
|
|
*/
|
2017-11-27 06:45:26 +03:00
|
|
|
static u3_weak
|
|
|
|
_http_vec_to_meth(h2o_iovec_t vec_u)
|
2017-11-22 09:55:39 +03:00
|
|
|
{
|
2017-11-25 00:32:11 +03:00
|
|
|
return ( 0 == strncmp(vec_u.base, "GET", vec_u.len) ) ? c3__get :
|
|
|
|
( 0 == strncmp(vec_u.base, "PUT", vec_u.len) ) ? c3__put :
|
|
|
|
( 0 == strncmp(vec_u.base, "POST", vec_u.len) ) ? c3__post :
|
|
|
|
( 0 == strncmp(vec_u.base, "HEAD", vec_u.len) ) ? c3__head :
|
|
|
|
( 0 == strncmp(vec_u.base, "CONNECT", vec_u.len) ) ? c3__conn :
|
|
|
|
( 0 == strncmp(vec_u.base, "DELETE", vec_u.len) ) ? c3__delt :
|
|
|
|
( 0 == strncmp(vec_u.base, "OPTIONS", vec_u.len) ) ? c3__opts :
|
|
|
|
( 0 == strncmp(vec_u.base, "TRACE", vec_u.len) ) ? c3__trac :
|
|
|
|
// TODO ??
|
|
|
|
// ( 0 == strncmp(vec_u.base, "PATCH", vec_u.len) ) ? c3__patc :
|
|
|
|
u3_none;
|
2017-11-22 09:55:39 +03:00
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_vec_to_atom(): convert h2o_iovec_t to atom (cord)
|
2017-11-22 09:55:39 +03:00
|
|
|
*/
|
|
|
|
static u3_noun
|
2017-11-27 06:45:26 +03:00
|
|
|
_http_vec_to_atom(h2o_iovec_t vec_u)
|
2017-11-22 09:55:39 +03:00
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
// XX portable?
|
2017-11-22 09:55:39 +03:00
|
|
|
return u3i_bytes(vec_u.len, (const c3_y*)vec_u.base);
|
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_vec_to_octs(): convert h2o_iovec_t to (unit octs)
|
2017-11-22 09:55:39 +03:00
|
|
|
*/
|
|
|
|
static u3_noun
|
2017-11-27 06:45:26 +03:00
|
|
|
_http_vec_to_octs(h2o_iovec_t vec_u)
|
2017-11-22 09:55:39 +03:00
|
|
|
{
|
|
|
|
if ( 0 == vec_u.len ) {
|
|
|
|
return u3_nul;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XX correct size_t -> atom?
|
|
|
|
return u3nt(u3_nul, u3i_chubs(1, (const c3_d*)&vec_u.len),
|
2017-11-27 06:45:26 +03:00
|
|
|
_http_vec_to_atom(vec_u));
|
2017-11-22 09:55:39 +03:00
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_vec_from_octs(): convert (unit octs) to h2o_iovec_t
|
2017-11-22 09:55:39 +03:00
|
|
|
*/
|
|
|
|
static h2o_iovec_t*
|
2017-11-27 06:45:26 +03:00
|
|
|
_http_vec_from_octs(u3_noun oct)
|
2017-11-22 09:55:39 +03:00
|
|
|
{
|
2017-11-25 00:31:49 +03:00
|
|
|
h2o_iovec_t* vec_u = c3_malloc(sizeof(*vec_u));
|
2017-11-22 09:55:39 +03:00
|
|
|
|
|
|
|
if ( u3_nul == oct ) {
|
2017-11-25 00:31:49 +03:00
|
|
|
vec_u->base = 0;
|
2017-11-22 09:55:39 +03:00
|
|
|
vec_u->len = 0;
|
|
|
|
}
|
|
|
|
else {
|
2017-11-25 00:31:49 +03:00
|
|
|
// XX bails
|
|
|
|
// if ( !_(u3a_is_cat(u3t(oct))) ) {
|
|
|
|
// // 2GB max
|
|
|
|
// u3m_bail(c3__fail); return 0;
|
|
|
|
// }
|
2017-11-22 09:55:39 +03:00
|
|
|
|
2017-11-25 00:31:49 +03:00
|
|
|
vec_u->len = u3h(u3t(oct));
|
|
|
|
vec_u->base = c3_malloc(vec_u->len);
|
|
|
|
u3r_bytes(0, vec_u->len, (c3_y*)vec_u->base, u3t(u3t(oct)));
|
2017-11-22 09:55:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
u3z(oct);
|
|
|
|
return vec_u;
|
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_heds_to_noun(): convert h2o_headers_t to (list (pair @t @t))
|
|
|
|
*/
|
|
|
|
static u3_noun
|
|
|
|
_http_heds_to_noun(h2o_headers_t* hed_u)
|
|
|
|
{
|
|
|
|
u3_noun hed = u3_nul;
|
|
|
|
size_t dex = hed_u->size;
|
|
|
|
|
|
|
|
h2o_header_t deh;
|
|
|
|
|
|
|
|
while ( 0 < dex ) {
|
|
|
|
deh = hed_u->entries[--dex];
|
|
|
|
hed = u3nc(u3nc(_http_vec_to_atom(*deh.name),
|
|
|
|
_http_vec_to_atom(deh.value)), hed);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* _http_heds_from_noun(): convert (list (pair @t @t)) to h2hed
|
|
|
|
*/
|
2017-11-25 01:12:09 +03:00
|
|
|
static h2hed*
|
2017-11-27 06:45:26 +03:00
|
|
|
_http_heds_from_noun(u3_noun hed)
|
2017-11-23 06:44:22 +03:00
|
|
|
{
|
2017-11-25 01:12:09 +03:00
|
|
|
u3_noun deh = hed;
|
|
|
|
h2hed* hed_u = 0;
|
|
|
|
|
|
|
|
while ( u3_nul != hed ) {
|
|
|
|
u3_noun nam = u3h(u3h(hed));
|
|
|
|
u3_noun val = u3t(u3h(hed));
|
2017-11-23 06:44:22 +03:00
|
|
|
|
2017-11-25 01:12:09 +03:00
|
|
|
h2hed* nex_u = c3_malloc(sizeof(*nex_u));
|
|
|
|
nex_u->nam_w = u3r_met(3, nam);
|
|
|
|
nex_u->val_w = u3r_met(3, val);
|
|
|
|
nex_u->nam_c = c3_malloc(nex_u->nam_w);
|
|
|
|
nex_u->val_c = c3_malloc(nex_u->val_w);
|
2017-11-23 06:44:22 +03:00
|
|
|
|
2017-11-25 01:12:09 +03:00
|
|
|
u3r_bytes(0, nex_u->nam_w, (c3_y*)nex_u->nam_c, nam);
|
|
|
|
u3r_bytes(0, nex_u->val_w, (c3_y*)nex_u->val_c, val);
|
2017-11-25 00:31:49 +03:00
|
|
|
|
2017-11-25 01:12:09 +03:00
|
|
|
nex_u->nex_u = hed_u;
|
|
|
|
hed_u = nex_u;
|
|
|
|
hed = u3t(hed);
|
|
|
|
}
|
2017-11-23 06:44:22 +03:00
|
|
|
|
2017-11-25 01:12:09 +03:00
|
|
|
u3z(deh);
|
|
|
|
return hed_u;
|
2017-11-23 06:44:22 +03:00
|
|
|
}
|
2017-11-22 09:55:39 +03:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_req_to_httq(): convert h2o_req_t to httq
|
2017-11-22 09:55:39 +03:00
|
|
|
*/
|
2017-11-27 06:45:26 +03:00
|
|
|
static u3_weak
|
|
|
|
_http_req_to_httq(h2o_req_t* rec_u)
|
2017-11-22 09:55:39 +03:00
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
u3_noun med = _http_vec_to_meth(rec_u->method);
|
2017-11-25 00:31:49 +03:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
if ( u3_none == med ) {
|
|
|
|
return u3_none;
|
2017-11-22 09:55:39 +03:00
|
|
|
}
|
2017-11-25 00:31:49 +03:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
u3_noun url = _http_vec_to_atom(rec_u->path_normalized);
|
|
|
|
u3_noun hed = _http_heds_to_noun(&rec_u->headers);
|
|
|
|
u3_noun bod = _http_vec_to_octs(rec_u->entity);
|
2017-11-22 09:55:39 +03:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
return u3nq(med, url, hed, bod);
|
2017-11-22 09:55:39 +03:00
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_req_free(): free http request.
|
2013-09-29 00:21:18 +04:00
|
|
|
*/
|
2014-08-21 02:09:51 +04:00
|
|
|
static void
|
2017-11-27 06:45:26 +03:00
|
|
|
_http_req_free(h2req* req_u)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
_http_req_unlink(req_u);
|
|
|
|
free(req_u);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_req_new(): receive http request.
|
2013-09-29 00:21:18 +04:00
|
|
|
*/
|
2017-11-27 06:45:26 +03:00
|
|
|
static h2req*
|
|
|
|
_http_req_new(h2con* hon_u, h2o_req_t* rec_u)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
// unnecessary, just an example
|
|
|
|
h2o_ctx_wrap* ctx_u = (h2o_ctx_wrap*)rec_u->conn->ctx;
|
|
|
|
h2htp* htp_u = ctx_u->htp_u;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
h2req* req_u = c3_malloc(sizeof(*req_u));
|
|
|
|
req_u->rec_u = rec_u;
|
|
|
|
_http_req_link(hon_u, req_u);
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
return req_u;
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_send_response(): write httr to h2o_req_t->res and send
|
2013-09-29 00:21:18 +04:00
|
|
|
*/
|
|
|
|
static void
|
2017-11-27 06:45:26 +03:00
|
|
|
_http_send_response(h2req* req_u, u3_noun sas, u3_noun hed, u3_noun bod)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
h2o_req_t* rec_u = req_u->rec_u;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
rec_u->res.status = sas;
|
|
|
|
rec_u->res.reason = (sas < 200) ? "Weird" :
|
|
|
|
(sas < 300) ? "OK" :
|
|
|
|
(sas < 400) ? "Moved" :
|
|
|
|
(sas < 500) ? "Missing" :
|
|
|
|
"Hosed";
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
h2hed* hed_u = _http_heds_from_noun(u3k(hed));
|
|
|
|
h2hed* deh_u = hed_u;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
while ( 0 != hed_u ) {
|
|
|
|
h2o_add_header_by_str(&rec_u->pool, &rec_u->res.headers,
|
|
|
|
hed_u->nam_c, hed_u->nam_w, 0, 0,
|
|
|
|
hed_u->val_c, hed_u->val_w);
|
|
|
|
hed_u = hed_u->nex_u;
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
// XX free req_u on disponse (rec_u should be free'd by h2o)
|
|
|
|
static h2o_generator_t gen_u = {NULL, NULL};
|
|
|
|
h2o_start_response(rec_u, &gen_u);
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
// TODO: put this in the generator so it runs on next turn?
|
|
|
|
h2o_iovec_t* bod_u = _http_vec_from_octs(u3k(bod));
|
|
|
|
h2o_send(rec_u, bod_u, 1, 1);
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
_http_req_free(req_u);
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
// XX allocate on &req_u->pool and skip these?
|
|
|
|
free(bod_u->base);
|
|
|
|
free(bod_u);
|
|
|
|
while ( 0 != deh_u ) {
|
|
|
|
h2hed* duh_u = deh_u;
|
|
|
|
deh_u = deh_u->nex_u;
|
|
|
|
free(duh_u->nam_c);
|
|
|
|
free(duh_u->val_c);
|
|
|
|
free(duh_u);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
u3z(sas); u3z(hed); u3z(bod);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
2013-12-19 00:42:29 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
// for casting and retrieving h2o_socket_t; see st_h2o_http1_conn_t
|
|
|
|
typedef struct _h2o_con_http1 {
|
|
|
|
h2o_conn_t con_u; // h2o connection
|
|
|
|
h2o_socket_t* sok_u; // h2o connection socket
|
|
|
|
} h2o_con_http1;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
// for casting and retrieving h2con; see st_h2o_uv_socket_t
|
|
|
|
typedef struct _h2o_sok_uv {
|
|
|
|
h2o_socket_t sok_u; // h2o connection socket
|
|
|
|
struct {
|
|
|
|
uv_stream_t *stream; // client stream handler (h2con)
|
|
|
|
uv_close_cb close_cb;
|
|
|
|
} uv;
|
|
|
|
} h2o_sok_uv;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_conn_from_req(); retrieve connection from h2o http1 request.
|
2013-09-29 00:21:18 +04:00
|
|
|
*/
|
2017-11-27 06:45:26 +03:00
|
|
|
static h2con*
|
|
|
|
_http_conn_from_req(h2o_req_t* rec_u)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
// XX HTTP2 wat do?
|
|
|
|
h2o_con_http1* noc_u = (h2o_con_http1*)rec_u->conn;
|
|
|
|
h2o_sok_uv* kos_u = (h2o_sok_uv*)noc_u->sok_u;
|
|
|
|
return (h2con*)kos_u->uv.stream;
|
|
|
|
};
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_handle_new_req(); handle incoming http request from h2o.
|
2013-09-29 00:21:18 +04:00
|
|
|
*/
|
2017-11-27 06:45:26 +03:00
|
|
|
static c3_i
|
|
|
|
_http_handle_new_req(h2o_handler_t* han_u, h2o_req_t* rec_u)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
u3_weak recq = _http_req_to_httq(rec_u);
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
if ( u3_none == recq ) {
|
|
|
|
// XX debug only
|
|
|
|
uL(fprintf(uH, "strange request\n"));
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
static h2o_generator_t gen_u = {NULL, NULL};
|
|
|
|
rec_u->res.status = 400;
|
|
|
|
rec_u->res.reason = "Bad Request";
|
|
|
|
h2o_start_response(rec_u, &gen_u);
|
|
|
|
h2o_send(rec_u, 0, 0, 1);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
2017-11-27 06:45:26 +03:00
|
|
|
else {
|
|
|
|
h2con* hon_u = _http_conn_from_req(rec_u);
|
|
|
|
h2req* req_u = _http_req_new(hon_u, rec_u);
|
|
|
|
_http_request(req_u, recq);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
return 0;
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
2014-05-26 22:02:39 +04:00
|
|
|
/* _http_conn_free_early(): free http connection on failure.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
_http_conn_free_early(uv_handle_t* han_t)
|
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
h2con* hon_u = (h2con*)han_t;
|
2014-05-26 22:02:39 +04:00
|
|
|
free(hon_u);
|
|
|
|
}
|
|
|
|
|
2013-09-29 00:21:18 +04:00
|
|
|
/* _http_conn_free(): free http connection on close.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
_http_conn_free(uv_handle_t* han_t)
|
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
h2con* hon_u = (h2con*)han_t;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
|
|
|
while ( 0 != hon_u->req_u ) {
|
2017-11-27 06:45:26 +03:00
|
|
|
h2req* req_u = hon_u->req_u;
|
|
|
|
h2req* nex_u = req_u->nex_u;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
_http_request_kill(req_u);
|
2013-09-29 00:21:18 +04:00
|
|
|
_http_req_free(req_u);
|
|
|
|
hon_u->req_u = nex_u;
|
|
|
|
}
|
2016-04-07 21:27:53 +03:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
_http_conn_unlink(hon_u);
|
2013-09-29 00:21:18 +04:00
|
|
|
free(hon_u);
|
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_conn_new(): create and accept http connection.
|
2013-09-29 00:21:18 +04:00
|
|
|
*/
|
|
|
|
static void
|
2017-11-27 06:45:26 +03:00
|
|
|
_http_conn_new(h2htp* htp_u)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
// TODO where?
|
|
|
|
// u3_lo_open();
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
h2con* hon_u = c3_malloc(sizeof(*hon_u));
|
|
|
|
hon_u->seq_l = 1;
|
|
|
|
hon_u->req_u = 0;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
uv_tcp_init(u3L, &hon_u->wax_u);
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
if ( 0 != uv_accept((uv_stream_t*)&htp_u->wax_u,
|
|
|
|
(uv_stream_t*)&hon_u->wax_u) ) {
|
|
|
|
uv_close((uv_handle_t*)&hon_u->wax_u,
|
|
|
|
(uv_close_cb)_http_conn_free_early);
|
|
|
|
return;
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
_http_conn_link(htp_u, hon_u);
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
hon_u->sok_u = h2o_uv_socket_create((uv_stream_t*)&hon_u->wax_u,
|
|
|
|
(uv_close_cb)_http_conn_free);
|
|
|
|
h2o_accept(htp_u->cep_u, hon_u->sok_u);
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
// capture h2o connection (XX fragile)
|
|
|
|
hon_u->con_u = (h2o_conn_t*)hon_u->sok_u->data;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
struct sockaddr_in adr_u;
|
|
|
|
h2o_socket_getpeername(hon_u->sok_u, (struct sockaddr*)&adr_u);
|
|
|
|
hon_u->ipf_w = ( adr_u.sin_family != AF_INET ) ?
|
|
|
|
0 : ntohl(adr_u.sin_addr.s_addr);
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
// TODO where?
|
|
|
|
// u3_lo_shut(c3y);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_listen_cb(): uv_connection_cb for uv_listen
|
2013-09-29 00:21:18 +04:00
|
|
|
*/
|
2017-11-27 06:45:26 +03:00
|
|
|
static void
|
|
|
|
_http_listen_cb(uv_stream_t* str_u, c3_i sas_i)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
h2htp* htp_u = (h2htp*)str_u;
|
2013-12-19 00:42:29 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
if ( 0 != sas_i ) {
|
|
|
|
// XX retrieve and print error?
|
|
|
|
uL(fprintf(uH, "http: listen_cb: ERROR\n"));
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
2014-03-03 23:51:35 +04:00
|
|
|
else {
|
2017-11-27 06:45:26 +03:00
|
|
|
_http_conn_new(htp_u);
|
2014-03-03 23:51:35 +04:00
|
|
|
}
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_req_find(): find http request in connection by sequence.
|
2013-09-29 00:21:18 +04:00
|
|
|
*/
|
2017-11-27 06:45:26 +03:00
|
|
|
static h2req*
|
|
|
|
_http_req_find(h2con* hon_u, c3_w seq_l)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
h2req* req_u = hon_u->req_u;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
// XX glories of linear search
|
|
|
|
//
|
|
|
|
while ( req_u ) {
|
|
|
|
if ( seq_l == req_u->seq_l ) {
|
|
|
|
return req_u;
|
2013-12-19 00:42:29 +04:00
|
|
|
}
|
2017-11-27 06:45:26 +03:00
|
|
|
req_u = req_u->nex_u;
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_req_link(): link http request to connection
|
2013-09-29 00:21:18 +04:00
|
|
|
*/
|
2017-11-27 06:45:26 +03:00
|
|
|
static void
|
|
|
|
_http_req_link(h2con* hon_u, h2req* req_u)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
|
|
|
req_u->hon_u = hon_u;
|
|
|
|
req_u->seq_l = hon_u->seq_l++;
|
2017-11-27 06:45:26 +03:00
|
|
|
req_u->nex_u = hon_u->req_u;
|
|
|
|
hon_u->req_u = req_u;
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_req_unlink(): remove http request from connection
|
2013-09-29 00:21:18 +04:00
|
|
|
*/
|
|
|
|
static void
|
2017-11-27 06:45:26 +03:00
|
|
|
_http_req_unlink(h2req* req_u)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
h2con* hon_u = req_u->hon_u;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
if ( hon_u->req_u == req_u ) {
|
|
|
|
hon_u->req_u = req_u->nex_u;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
h2req* pre_u = hon_u->req_u;
|
|
|
|
|
|
|
|
// XX glories of linear search
|
|
|
|
//
|
|
|
|
while ( pre_u ) {
|
|
|
|
if ( pre_u->nex_u == req_u ) {
|
|
|
|
pre_u->nex_u = req_u->nex_u;
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
2017-11-27 06:45:26 +03:00
|
|
|
else pre_u = pre_u->nex_u;
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_conn_find(): find http connection in server by sequence.
|
|
|
|
*/
|
|
|
|
static h2con*
|
|
|
|
_http_conn_find(h2htp *htp_u, c3_w coq_l)
|
2017-11-22 09:55:39 +03:00
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
h2con* hon_u = htp_u->hon_u;
|
2017-11-25 00:31:49 +03:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
// XX glories of linear search
|
|
|
|
//
|
|
|
|
while ( hon_u ) {
|
|
|
|
if ( coq_l == hon_u->coq_l ) {
|
|
|
|
return hon_u;
|
|
|
|
}
|
|
|
|
hon_u = hon_u->nex_u;
|
2017-11-25 00:31:49 +03:00
|
|
|
}
|
2017-11-22 09:55:39 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_conn_link(): link http request to connection
|
2017-11-23 06:43:42 +03:00
|
|
|
*/
|
|
|
|
static void
|
2017-11-27 06:45:26 +03:00
|
|
|
_http_conn_link(h2htp* htp_u, h2con* hon_u)
|
2017-11-22 09:55:39 +03:00
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
hon_u->htp_u = htp_u;
|
|
|
|
hon_u->coq_l = htp_u->coq_l++;
|
|
|
|
hon_u->nex_u = htp_u->hon_u;
|
|
|
|
htp_u->hon_u = hon_u;
|
2017-11-22 09:55:39 +03:00
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_conn_unlink(): remove http request from connection
|
2013-09-29 00:21:18 +04:00
|
|
|
*/
|
|
|
|
static void
|
2017-11-27 06:45:26 +03:00
|
|
|
_http_conn_unlink(h2con* hon_u)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
h2htp* htp_u = hon_u->htp_u;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
if ( htp_u->hon_u == hon_u ) {
|
|
|
|
htp_u->hon_u = hon_u->nex_u;
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
else {
|
2017-11-27 06:45:26 +03:00
|
|
|
h2con *pre_u = htp_u->hon_u;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
// XX glories of linear search
|
|
|
|
//
|
|
|
|
while ( pre_u ) {
|
|
|
|
if ( pre_u->nex_u == hon_u ) {
|
|
|
|
pre_u->nex_u = hon_u->nex_u;
|
|
|
|
}
|
|
|
|
else pre_u = pre_u->nex_u;
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_serv_find(): find http server by sequence.
|
2014-03-20 02:40:40 +04:00
|
|
|
*/
|
2017-11-27 06:45:26 +03:00
|
|
|
static h2htp*
|
2014-03-20 02:40:40 +04:00
|
|
|
_http_serv_find(c3_l sev_l)
|
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
h2htp* htp_u = sev_u; // u3_Host.htp_u;
|
2014-03-20 02:40:40 +04:00
|
|
|
|
|
|
|
// XX glories of linear search
|
|
|
|
//
|
|
|
|
while ( htp_u ) {
|
|
|
|
if ( sev_l == htp_u->sev_l ) {
|
|
|
|
return htp_u;
|
|
|
|
}
|
|
|
|
htp_u = htp_u->nex_u;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
// XX serv_link and serv_unlink
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
// XX rename
|
2013-09-29 00:21:18 +04:00
|
|
|
/* _http_pox_to_noun(): translate srv/con/req to path noun (pox).
|
|
|
|
*/
|
2014-09-11 04:01:32 +04:00
|
|
|
static u3_noun
|
2013-09-29 00:21:18 +04:00
|
|
|
_http_pox_to_noun(c3_w sev_l, c3_w coq_l, c3_w seq_l)
|
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
return u3nt(u3_blip, c3__http,
|
|
|
|
u3nq(u3dc("scot", c3_s2('u','v'), sev_l),
|
|
|
|
u3dc("scot", c3_s2('u','d'), coq_l),
|
|
|
|
u3dc("scot", c3_s2('u','d'), seq_l),
|
|
|
|
u3_nul));
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_request(): dispatch http request to %eyre
|
2013-09-29 00:21:18 +04:00
|
|
|
*/
|
2017-11-27 06:45:26 +03:00
|
|
|
static void
|
|
|
|
_http_request(h2req* req_u, u3_noun recq)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
#if 1
|
|
|
|
uL(fprintf(uH, "new request:\n"));
|
|
|
|
u3_noun span = u3v_wish("-:!>(*{@t @t (list {p/@t q/@t}) (unit {p/@ q/@})})");
|
|
|
|
u3m_tape(u3dc("text", span, u3k(recq)));
|
|
|
|
uL(fprintf(uH, "\n"));
|
|
|
|
#endif
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
u3_noun pox = _http_pox_to_noun(req_u->hon_u->htp_u->sev_l,
|
|
|
|
req_u->hon_u->coq_l,
|
|
|
|
req_u->seq_l);
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
u3_noun typ = _(req_u->hon_u->htp_u->lop) ? c3__chis : c3__this;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
u3v_plan(pox, u3nq(typ,
|
|
|
|
req_u->hon_u->htp_u->sec,
|
|
|
|
u3nc(c3y, u3i_words(1, &req_u->hon_u->ipf_w)),
|
|
|
|
recq));
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_request_kill(): kill http request in %eyre.
|
2014-04-14 22:01:18 +04:00
|
|
|
*/
|
|
|
|
static void
|
2017-11-27 06:45:26 +03:00
|
|
|
_http_request_kill(h2req* req_u)
|
2014-04-14 22:01:18 +04:00
|
|
|
{
|
2014-09-11 04:01:32 +04:00
|
|
|
u3_noun pox = _http_pox_to_noun(req_u->hon_u->htp_u->sev_l,
|
2014-04-14 22:01:18 +04:00
|
|
|
req_u->hon_u->coq_l,
|
|
|
|
req_u->seq_l);
|
|
|
|
|
2014-11-06 03:20:01 +03:00
|
|
|
u3v_plan(pox, u3nc(c3__thud, u3_nul));
|
2014-04-14 22:01:18 +04:00
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_respond(): attach %eyre response to open request.
|
2013-09-29 00:21:18 +04:00
|
|
|
*/
|
|
|
|
static void
|
2017-11-27 06:45:26 +03:00
|
|
|
_http_respond(c3_l sev_l, c3_l coq_l, c3_l seq_l, u3_noun rep)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
h2htp* htp_u;
|
|
|
|
h2con* hon_u;
|
|
|
|
h2req* req_u;
|
2013-12-19 00:42:29 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
if ( !(htp_u = _http_serv_find(sev_l)) ) {
|
2015-11-19 05:31:12 +03:00
|
|
|
if ( (u3C.wag_w & u3o_verbose) ) {
|
2017-11-27 06:45:26 +03:00
|
|
|
uL(fprintf(uH, "http: server not found: %x\r\n", sev_l));
|
2015-11-19 05:31:12 +03:00
|
|
|
}
|
2017-11-27 06:45:26 +03:00
|
|
|
u3z(rep);
|
2014-03-28 22:43:35 +04:00
|
|
|
return;
|
2014-03-20 02:40:40 +04:00
|
|
|
}
|
2017-11-27 06:45:26 +03:00
|
|
|
if ( !(hon_u = _http_conn_find(htp_u, coq_l)) ) {
|
2015-11-19 05:31:12 +03:00
|
|
|
if ( (u3C.wag_w & u3o_verbose) ) {
|
2017-11-27 06:45:26 +03:00
|
|
|
uL(fprintf(uH, "http: connection not found: %x/%d\r\n", sev_l, coq_l));
|
2015-11-19 05:31:12 +03:00
|
|
|
}
|
2017-11-27 06:45:26 +03:00
|
|
|
u3z(rep);
|
2013-09-29 00:21:18 +04:00
|
|
|
return;
|
|
|
|
}
|
2017-11-27 06:45:26 +03:00
|
|
|
if ( !(req_u = _http_req_find(hon_u, seq_l)) ) {
|
2015-11-19 05:31:12 +03:00
|
|
|
if ( (u3C.wag_w & u3o_verbose) ) {
|
2017-11-27 06:45:26 +03:00
|
|
|
uL(fprintf(uH, "http: request not found: %x/%d/%d\r\n", sev_l, coq_l, seq_l));
|
2015-11-19 05:31:12 +03:00
|
|
|
}
|
2017-11-27 06:45:26 +03:00
|
|
|
u3z(rep);
|
2013-09-29 00:21:18 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
{
|
|
|
|
u3_noun p_rep, q_rep, r_rep;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
if ( c3n == u3r_trel(rep, &p_rep, &q_rep, &r_rep) ) {
|
|
|
|
uL(fprintf(uH, "http: strange response\n"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_http_send_response(req_u, u3k(p_rep), u3k(q_rep), u3k(r_rep));
|
|
|
|
}
|
|
|
|
}
|
2013-10-30 22:42:24 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
u3z(rep);
|
2013-10-30 22:42:24 +04:00
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_init_h2o(): initialize h2o ctx and handlers for server.
|
2013-09-29 00:21:18 +04:00
|
|
|
*/
|
2017-11-27 06:45:26 +03:00
|
|
|
static void
|
|
|
|
_http_init_h2o(h2htp* htp_u)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
// wrapped for server backlink (wrapper unnecessary, just an example)
|
|
|
|
htp_u->ctx_u = c3_malloc(sizeof(h2o_ctx_wrap));
|
|
|
|
memset(htp_u->ctx_u, 0, sizeof(h2o_ctx_wrap));
|
|
|
|
h2o_ctx_wrap* ctx_u = (h2o_ctx_wrap*)htp_u->ctx_u;
|
|
|
|
ctx_u->htp_u = htp_u;
|
2017-11-25 00:31:49 +03:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
//// FAILED ATTEMPTS:
|
|
|
|
// can't wrap handler and link server, see pointer math in request.c call_handlers
|
|
|
|
// can't use htp_u->ctx_u->_module_configs (don't know why)
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
htp_u->cep_u = c3_malloc(sizeof(*htp_u->cep_u));
|
|
|
|
memset(htp_u->cep_u, 0, sizeof(*htp_u->cep_u));
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
htp_u->cep_u->ctx = (h2o_context_t*)htp_u->ctx_u;
|
|
|
|
htp_u->cep_u->hosts = fig_u.hosts;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
// XX read name from server?
|
|
|
|
htp_u->hos_u = h2o_config_register_host(&fig_u,
|
|
|
|
h2o_iovec_init(H2O_STRLIT("default")),
|
|
|
|
htp_u->por_w);
|
|
|
|
|
|
|
|
// XX attach to server?
|
|
|
|
h2o_handler_t* han_u = h2o_create_handler(&htp_u->hos_u->fallback_path, sizeof(*han_u));
|
|
|
|
han_u->on_req = _http_handle_new_req;
|
|
|
|
|
|
|
|
// XX handler lifecycle
|
|
|
|
// han_u->on_context_init
|
|
|
|
// han_u->on_context_dispose
|
|
|
|
// han_u->dispose
|
|
|
|
|
|
|
|
h2o_context_init((h2o_context_t*)htp_u->ctx_u, u3L, &fig_u);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* _http_start(): start http server.
|
|
|
|
*/
|
|
|
|
static void
|
2017-11-27 06:45:26 +03:00
|
|
|
_http_start(h2htp* htp_u)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
struct sockaddr_in adr_u;
|
|
|
|
memset(&adr_u, 0, sizeof(adr_u));
|
|
|
|
adr_u.sin_family = AF_INET;
|
2016-02-11 22:44:28 +03:00
|
|
|
|
|
|
|
if ( c3y == htp_u->lop ) {
|
2017-11-27 06:45:26 +03:00
|
|
|
inet_pton(AF_INET, "127.0.0.1", &adr_u.sin_addr);
|
2016-02-11 22:44:28 +03:00
|
|
|
}
|
|
|
|
else {
|
2017-11-27 06:45:26 +03:00
|
|
|
adr_u.sin_addr.s_addr = INADDR_ANY;
|
2016-02-11 22:44:28 +03:00
|
|
|
}
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
uv_tcp_init(u3L, &htp_u->wax_u);
|
|
|
|
|
2013-09-29 00:21:18 +04:00
|
|
|
/* Try ascending ports.
|
|
|
|
*/
|
|
|
|
while ( 1 ) {
|
2017-11-27 06:45:26 +03:00
|
|
|
c3_i ret;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
adr_u.sin_port = htons(htp_u->por_w);
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
// XX maybe don't check uv_tcp_bind ret
|
|
|
|
if ( 0 != (ret = uv_tcp_bind(&htp_u->wax_u, (const struct sockaddr*)&adr_u, 0)) ||
|
|
|
|
0 != (ret = uv_listen((uv_stream_t*)&htp_u->wax_u,
|
|
|
|
TCP_BACKLOG, _http_listen_cb)) ) {
|
2014-08-21 02:09:51 +04:00
|
|
|
if ( UV_EADDRINUSE == ret ) {
|
2013-12-19 00:42:29 +04:00
|
|
|
htp_u->por_w++;
|
2013-09-29 00:21:18 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
2014-08-21 02:09:51 +04:00
|
|
|
uL(fprintf(uH, "http: listen: %s\n", uv_strerror(ret)));
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
}
|
2017-11-27 06:45:26 +03:00
|
|
|
|
2016-02-11 22:44:28 +03:00
|
|
|
uL(fprintf(uH, "http: live (%s, %s) on %d\n",
|
2014-11-05 04:18:47 +03:00
|
|
|
(c3y == htp_u->sec) ? "\"secure\"" : "insecure",
|
2016-02-11 22:44:28 +03:00
|
|
|
(c3y == htp_u->lop) ? "loopback" : "public",
|
2014-03-20 02:40:40 +04:00
|
|
|
htp_u->por_w));
|
2017-11-27 06:45:26 +03:00
|
|
|
|
|
|
|
_http_init_h2o(htp_u);
|
2013-09-29 00:21:18 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-12 23:37:43 +03:00
|
|
|
/* _http_write_ports_file(): update .http.ports
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
_http_write_ports_file(c3_c *pax_c)
|
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
#if 0
|
|
|
|
c3_i pal_i;
|
2016-12-13 16:31:33 +03:00
|
|
|
c3_c *paf_c;
|
2017-11-27 06:45:26 +03:00
|
|
|
c3_i por_i;
|
2016-12-12 23:37:43 +03:00
|
|
|
u3_http *htp_u;
|
|
|
|
|
2016-12-13 16:31:33 +03:00
|
|
|
pal_i = strlen(pax_c) + 13; /* includes NUL */
|
|
|
|
paf_c = u3a_malloc(pal_i);
|
|
|
|
snprintf(paf_c, pal_i, "%s/%s", pax_c, ".http.ports");
|
2016-12-12 23:37:43 +03:00
|
|
|
|
2016-12-13 16:31:33 +03:00
|
|
|
por_i = open(paf_c, O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
|
|
|
u3a_free(paf_c);
|
2016-12-12 23:37:43 +03:00
|
|
|
|
|
|
|
for ( htp_u = u3_Host.htp_u; htp_u; htp_u = htp_u->nex_u ) {
|
2016-12-13 16:31:33 +03:00
|
|
|
dprintf(por_i, "%u %s %s\n", htp_u->por_w,
|
2016-12-13 01:39:56 +03:00
|
|
|
(c3y == htp_u->sec) ? "assumed-secure" : "insecure",
|
2016-12-12 23:37:43 +03:00
|
|
|
(c3y == htp_u->lop) ? "loopback" : "public");
|
2016-12-13 16:31:33 +03:00
|
|
|
}
|
2016-12-12 23:37:43 +03:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
c3_sync(por_i);
|
|
|
|
close(por_i);
|
|
|
|
#endif
|
2016-12-12 23:37:43 +03:00
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* _http_release_ports_file(): remove .http.ports
|
2016-12-12 23:37:43 +03:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
_http_release_ports_file(c3_c *pax_c)
|
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
#if 0
|
|
|
|
c3_i pal_i;
|
2016-12-13 16:31:33 +03:00
|
|
|
c3_c *paf_c;
|
2016-12-12 23:37:43 +03:00
|
|
|
|
2016-12-13 16:31:33 +03:00
|
|
|
pal_i = strlen(pax_c) + 13; /* includes NUL */
|
|
|
|
paf_c = u3a_malloc(pal_i);
|
|
|
|
snprintf(paf_c, pal_i, "%s/%s", pax_c, ".http.ports");
|
2016-12-12 23:37:43 +03:00
|
|
|
|
|
|
|
unlink(paf_c);
|
2017-06-27 14:02:08 +03:00
|
|
|
u3a_free(paf_c);
|
2017-11-27 06:45:26 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// XX rename (is a card an effect?)
|
|
|
|
/* u3_http_ef_bake(): notify %eyre that we're live
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
u3_http_ef_bake(void)
|
|
|
|
{
|
|
|
|
u3_noun pax = u3nq(u3_blip, c3__http, u3k(u3A->sen), u3_nul);
|
|
|
|
|
|
|
|
u3v_plan(pax, u3nc(c3__born, u3_nul));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* u3_http_ef_thou(): send %thou from %eyre as http response.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
u3_http_ef_thou(c3_l sev_l,
|
|
|
|
c3_l coq_l,
|
|
|
|
c3_l seq_l,
|
|
|
|
u3_noun rep)
|
|
|
|
{
|
|
|
|
_http_respond(sev_l, coq_l, seq_l, u3k(rep));
|
|
|
|
u3z(rep);
|
2016-12-12 23:37:43 +03:00
|
|
|
}
|
|
|
|
|
2014-09-11 04:01:32 +04:00
|
|
|
/* u3_http_io_init(): initialize http I/O.
|
2013-09-29 00:21:18 +04:00
|
|
|
*/
|
2013-12-19 00:42:29 +04:00
|
|
|
void
|
2014-09-11 04:01:32 +04:00
|
|
|
u3_http_io_init()
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
#if 0
|
2016-02-11 22:44:28 +03:00
|
|
|
// Lens port
|
|
|
|
{
|
|
|
|
u3_http *htp_u = c3_malloc(sizeof(*htp_u));
|
|
|
|
|
|
|
|
htp_u->sev_l = u3A->sev_l + 2;
|
|
|
|
htp_u->coq_l = 1;
|
|
|
|
htp_u->por_w = 12321;
|
|
|
|
htp_u->sec = c3n;
|
|
|
|
htp_u->lop = c3y;
|
|
|
|
|
|
|
|
htp_u->hon_u = 0;
|
|
|
|
htp_u->nex_u = 0;
|
|
|
|
|
|
|
|
htp_u->nex_u = u3_Host.htp_u;
|
|
|
|
u3_Host.htp_u = htp_u;
|
|
|
|
}
|
|
|
|
|
2014-03-20 02:40:40 +04:00
|
|
|
// Logically secure port.
|
|
|
|
{
|
2014-09-11 04:01:32 +04:00
|
|
|
u3_http *htp_u = c3_malloc(sizeof(*htp_u));
|
2014-03-20 02:40:40 +04:00
|
|
|
|
2014-09-11 04:01:32 +04:00
|
|
|
htp_u->sev_l = u3A->sev_l + 1;
|
2014-03-20 02:40:40 +04:00
|
|
|
htp_u->coq_l = 1;
|
|
|
|
htp_u->por_w = 8443;
|
2014-11-05 04:18:47 +03:00
|
|
|
htp_u->sec = c3y;
|
2016-02-11 22:44:28 +03:00
|
|
|
htp_u->lop = c3n;
|
2014-03-20 02:40:40 +04:00
|
|
|
|
|
|
|
htp_u->hon_u = 0;
|
|
|
|
htp_u->nex_u = 0;
|
|
|
|
|
2014-09-11 04:01:32 +04:00
|
|
|
htp_u->nex_u = u3_Host.htp_u;
|
|
|
|
u3_Host.htp_u = htp_u;
|
2014-03-20 02:40:40 +04:00
|
|
|
}
|
2017-11-27 06:45:26 +03:00
|
|
|
#endif
|
|
|
|
// Insecure port.
|
2014-03-20 02:40:40 +04:00
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
h2htp* htp_u = c3_malloc(sizeof(*htp_u));
|
2014-03-20 02:40:40 +04:00
|
|
|
|
2014-09-11 04:01:32 +04:00
|
|
|
htp_u->sev_l = u3A->sev_l;
|
2014-03-20 02:40:40 +04:00
|
|
|
htp_u->coq_l = 1;
|
|
|
|
htp_u->por_w = 8080;
|
2014-11-05 04:18:47 +03:00
|
|
|
htp_u->sec = c3n;
|
2016-02-11 22:44:28 +03:00
|
|
|
htp_u->lop = c3n;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
htp_u->cep_u = 0;
|
|
|
|
htp_u->hos_u = 0;
|
2014-03-20 02:40:40 +04:00
|
|
|
htp_u->hon_u = 0;
|
|
|
|
htp_u->nex_u = 0;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
// XX u3_Host.htp_u
|
|
|
|
htp_u->nex_u = sev_u;
|
|
|
|
sev_u = htp_u;
|
2014-03-20 02:40:40 +04:00
|
|
|
}
|
2014-02-27 04:40:53 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
// XX why is this here?
|
2014-09-11 04:01:32 +04:00
|
|
|
u3_Host.ctp_u.coc_u = 0;
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
/* u3_http_io_talk(): start http I/O.
|
2014-01-17 12:12:05 +04:00
|
|
|
*/
|
|
|
|
void
|
2014-09-11 04:01:32 +04:00
|
|
|
u3_http_io_talk()
|
2014-01-17 12:12:05 +04:00
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
h2htp* htp_u;
|
2014-01-17 12:12:05 +04:00
|
|
|
|
2017-11-27 06:45:26 +03:00
|
|
|
// XX "global" per server?
|
|
|
|
h2o_config_init(&fig_u);
|
|
|
|
|
|
|
|
// XX u3_Host.htp_u
|
|
|
|
for ( htp_u = sev_u; htp_u; htp_u = htp_u->nex_u ) {
|
2014-03-20 02:40:40 +04:00
|
|
|
_http_start(htp_u);
|
|
|
|
}
|
2016-12-12 23:37:43 +03:00
|
|
|
|
|
|
|
_http_write_ports_file(u3_Host.dir_c);
|
2014-01-17 12:12:05 +04:00
|
|
|
}
|
|
|
|
|
2014-09-11 04:01:32 +04:00
|
|
|
/* u3_http_io_poll(): poll kernel for http I/O.
|
2013-09-29 00:21:18 +04:00
|
|
|
*/
|
|
|
|
void
|
2014-09-11 04:01:32 +04:00
|
|
|
u3_http_io_poll(void)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-09-11 04:01:32 +04:00
|
|
|
/* u3_http_io_exit(): shut down http.
|
2013-09-29 00:21:18 +04:00
|
|
|
*/
|
|
|
|
void
|
2014-09-11 04:01:32 +04:00
|
|
|
u3_http_io_exit(void)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2017-11-27 06:45:26 +03:00
|
|
|
// XX shutdown servers cleanly
|
2016-12-12 23:37:43 +03:00
|
|
|
_http_release_ports_file(u3_Host.dir_c);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|