shrub/vere/cttp.c

940 lines
20 KiB
C
Raw Normal View History

2014-02-27 04:39:48 +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 <setjmp.h>
#include <gmp.h>
#include <stdint.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <uv.h>
#include <errno.h>
#include <curses.h>
#include <termios.h>
#include <term.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
2014-02-27 04:39:48 +04:00
#include "all.h"
#include "vere/vere.h"
2014-02-27 04:39:48 +04:00
2018-03-12 05:57:16 +03:00
#include "h2o.h"
2014-02-27 04:39:48 +04:00
/* _cttp_bod(): create a data buffer.
*/
2014-09-11 04:01:32 +04:00
static u3_hbod*
_cttp_bod(c3_w len_w, c3_c* hun_c)
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
u3_hbod* bod_u = c3_malloc(len_w + sizeof(*bod_u));
2014-02-27 04:39:48 +04:00
bod_u->len_w = len_w;
memcpy(bod_u->hun_y, (const c3_y*)hun_c, len_w);
2014-02-27 04:39:48 +04:00
bod_u->nex_u = 0;
return bod_u;
}
/* _cttp_bud(): create a header buffer. Not null-terminated!
*/
2014-09-11 04:01:32 +04:00
static u3_hbod*
_cttp_bud(u3_hhed* hed_u)
2014-02-27 04:39:48 +04:00
{
c3_w len_w = hed_u->nam_w + 2 + hed_u->val_w + 2;
2014-09-11 04:01:32 +04:00
u3_hbod* bod_u = c3_malloc(len_w + sizeof(*bod_u));
2014-02-27 04:39:48 +04:00
memcpy(bod_u->hun_y, hed_u->nam_c, hed_u->nam_w);
memcpy(bod_u->hun_y + hed_u->nam_w, ": ", 2);
memcpy(bod_u->hun_y + hed_u->nam_w + 2, hed_u->val_c, hed_u->val_w);
memcpy(bod_u->hun_y + hed_u->nam_w + 2 + hed_u->val_w, "\r\n", 2);
2014-02-27 04:39:48 +04:00
bod_u->len_w = len_w;
bod_u->nex_u = 0;
return bod_u;
}
// XX deduplicate with _http_heds_free
/* _cttp_heds_free(): free header linked list
2014-02-27 04:39:48 +04:00
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_heds_free(u3_hhed* hed_u)
2014-02-27 04:39:48 +04:00
{
2014-05-15 02:12:04 +04:00
while ( hed_u ) {
2014-09-11 04:01:32 +04:00
u3_hhed* nex_u = hed_u->nex_u;
2014-02-27 04:39:48 +04:00
free(hed_u);
hed_u = nex_u;
}
}
/* _cttp_bods_free(): free body structure.
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_bods_free(u3_hbod* bod_u)
2014-02-27 04:39:48 +04:00
{
2014-05-15 02:12:04 +04:00
while ( bod_u ) {
2014-09-11 04:01:32 +04:00
u3_hbod* nex_u = bod_u->nex_u;
2014-02-27 04:39:48 +04:00
free(bod_u);
bod_u = nex_u;
}
}
/* _cttp_bods_to_octs: translate body into octet-stream noun.
*/
2014-09-11 04:01:32 +04:00
static u3_noun
_cttp_bods_to_octs(u3_hbod* bod_u)
2014-02-27 04:39:48 +04:00
{
c3_w len_w;
c3_y* buf_y;
2014-09-11 04:01:32 +04:00
u3_noun cos;
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
u3_hbod* bid_u;
2014-02-27 04:39:48 +04:00
len_w = 0;
for ( bid_u = bod_u; bid_u; bid_u = bid_u->nex_u ) {
len_w += bid_u->len_w;
}
}
2014-04-02 04:47:01 +04:00
buf_y = c3_malloc(len_w);
2014-02-27 04:39:48 +04:00
{
c3_y* ptr_y = buf_y;
while ( bod_u ) {
memcpy(ptr_y, bod_u->hun_y, bod_u->len_w);
ptr_y += bod_u->len_w;
bod_u = bod_u->nex_u;
}
}
2014-11-06 03:20:01 +03:00
cos = u3i_bytes(len_w, buf_y);
2014-02-27 04:39:48 +04:00
free(buf_y);
2014-09-11 04:01:32 +04:00
return u3nc(len_w, cos);
2014-02-27 04:39:48 +04:00
}
// XX deduplicate with _http_hed_new
/* _cttp_hed_new(): create u3_hhed from nam/val cords
*/
2014-09-11 04:01:32 +04:00
static u3_hhed*
_cttp_hed_new(u3_atom nam, u3_atom val)
2014-02-27 04:39:48 +04:00
{
c3_w nam_w = u3r_met(3, nam);
c3_w val_w = u3r_met(3, val);
u3_hhed* hed_u = c3_malloc(nam_w + val_w + sizeof(*hed_u));
2014-02-27 04:39:48 +04:00
hed_u->nex_u = 0;
hed_u->nam_w = nam_w;
hed_u->val_w = val_w;
u3r_bytes(0, nam_w, (c3_y*)hed_u->nam_c, nam);
u3r_bytes(0, val_w, (c3_y*)hed_u->val_c, val);
2014-02-27 04:39:48 +04:00
return hed_u;
}
// XX vv similar to _http_heds_from_noun
/* _cttp_heds_math(): create headers from ++math
2014-02-27 04:39:48 +04:00
*/
2014-09-11 04:01:32 +04:00
static u3_hhed*
_cttp_heds_math(u3_noun mah)
2014-02-27 04:39:48 +04:00
{
u3_noun hed = u3kdi_tap(mah);
u3_noun deh = hed;
u3_hhed* hed_u = 0;
while ( u3_nul != hed ) {
u3_noun nam = u3h(u3h(hed));
u3_noun lit = u3t(u3h(hed));
while ( u3_nul != lit ) {
u3_hhed* nex_u = _cttp_hed_new(nam, u3h(lit));
nex_u->nex_u = hed_u;
hed_u = nex_u;
lit = u3t(lit);
}
hed = u3t(hed);
2014-02-27 04:39:48 +04:00
}
u3z(deh);
return hed_u;
2014-02-27 04:39:48 +04:00
}
/* _cttp_octs_to_bod(): translate octet-stream noun into body.
*/
2014-09-11 04:01:32 +04:00
static u3_hbod*
_cttp_octs_to_bod(u3_noun oct)
2014-02-27 04:39:48 +04:00
{
c3_w len_w;
2014-11-06 03:20:01 +03:00
if ( !_(u3a_is_cat(u3h(oct))) ) { // 2GB max
u3m_bail(c3__fail); return 0;
2014-02-27 04:39:48 +04:00
}
2014-09-11 04:01:32 +04:00
len_w = u3h(oct);
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
u3_hbod* bod_u = c3_malloc(len_w + sizeof(*bod_u));
2014-02-27 04:39:48 +04:00
bod_u->len_w = len_w;
2014-11-06 03:20:01 +03:00
u3r_bytes(0, len_w, bod_u->hun_y, u3t(oct));
2014-02-27 04:39:48 +04:00
bod_u->nex_u = 0;
2014-09-11 04:01:32 +04:00
u3z(oct);
2014-02-27 04:39:48 +04:00
return bod_u;
}
}
/* _cttp_mcut_char(): measure/cut character.
*/
static c3_w
_cttp_mcut_char(c3_c* buf_c, c3_w len_w, c3_c chr_c)
{
if ( buf_c ) {
buf_c[len_w] = chr_c;
}
return len_w + 1;
}
/* _cttp_mcut_cord(): measure/cut cord.
2014-02-27 04:39:48 +04:00
*/
static c3_w
_cttp_mcut_cord(c3_c* buf_c, c3_w len_w, u3_noun san)
2014-02-27 04:39:48 +04:00
{
2014-11-06 03:20:01 +03:00
c3_w ten_w = u3r_met(3, san);
2014-02-27 04:39:48 +04:00
if ( buf_c ) {
2014-11-06 03:20:01 +03:00
u3r_bytes(0, ten_w, (c3_y *)(buf_c + len_w), san);
2014-02-27 04:39:48 +04:00
}
2014-09-11 04:01:32 +04:00
u3z(san);
2014-02-27 04:39:48 +04:00
return (len_w + ten_w);
}
/* _cttp_mcut_path(): measure/cut cord list.
2014-02-27 04:39:48 +04:00
*/
static c3_w
2014-09-11 04:01:32 +04:00
_cttp_mcut_path(c3_c* buf_c, c3_w len_w, c3_c sep_c, u3_noun pax)
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
u3_noun axp = pax;
2014-02-27 04:39:48 +04:00
2014-09-11 04:01:32 +04:00
while ( u3_nul != axp ) {
u3_noun h_axp = u3h(axp);
2014-02-27 04:39:48 +04:00
len_w = _cttp_mcut_cord(buf_c, len_w, u3k(h_axp));
2014-09-11 04:01:32 +04:00
axp = u3t(axp);
2014-02-27 05:58:40 +04:00
2014-09-11 04:01:32 +04:00
if ( u3_nul != axp ) {
2014-02-27 05:58:40 +04:00
len_w = _cttp_mcut_char(buf_c, len_w, sep_c);
}
2014-02-27 04:39:48 +04:00
}
2014-09-11 04:01:32 +04:00
u3z(pax);
2014-02-27 04:39:48 +04:00
return len_w;
}
/* _cttp_mcut_host(): measure/cut host.
*/
static c3_w
2014-09-11 04:01:32 +04:00
_cttp_mcut_host(c3_c* buf_c, c3_w len_w, u3_noun hot)
2014-02-27 04:39:48 +04:00
{
len_w = _cttp_mcut_path(buf_c, len_w, '.', u3kb_flop(u3k(hot)));
2014-09-11 04:01:32 +04:00
u3z(hot);
2014-02-27 04:39:48 +04:00
return len_w;
}
2014-03-14 21:47:17 +04:00
/* _cttp_mcut_pork(): measure/cut path/extension.
2014-02-27 04:39:48 +04:00
*/
static c3_w
2014-09-11 04:01:32 +04:00
_cttp_mcut_pork(c3_c* buf_c, c3_w len_w, u3_noun pok)
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
u3_noun h_pok = u3h(pok);
u3_noun t_pok = u3t(pok);
2014-02-27 04:39:48 +04:00
2014-09-11 04:01:32 +04:00
len_w = _cttp_mcut_path(buf_c, len_w, '/', u3k(t_pok));
if ( u3_nul != h_pok ) {
2014-02-27 04:39:48 +04:00
len_w = _cttp_mcut_char(buf_c, len_w, '.');
len_w = _cttp_mcut_cord(buf_c, len_w, u3k(u3t(h_pok)));
2014-02-27 04:39:48 +04:00
}
2014-09-11 04:01:32 +04:00
u3z(pok);
2014-02-27 04:39:48 +04:00
return len_w;
}
/* _cttp_mcut_quay(): measure/cut query.
*/
static c3_w
2014-09-11 04:01:32 +04:00
_cttp_mcut_quay(c3_c* buf_c, c3_w len_w, u3_noun quy)
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
if ( u3_nul == quy ) {
2014-02-27 04:39:48 +04:00
return len_w;
}
else {
2014-09-11 04:01:32 +04:00
u3_noun i_quy = u3h(quy);
u3_noun pi_quy = u3h(i_quy);
u3_noun qi_quy = u3t(i_quy);
u3_noun t_quy = u3t(quy);
2014-02-27 04:39:48 +04:00
len_w = _cttp_mcut_char(buf_c, len_w, '&');
len_w = _cttp_mcut_cord(buf_c, len_w, u3k(pi_quy));
2014-02-27 04:39:48 +04:00
len_w = _cttp_mcut_char(buf_c, len_w, '=');
len_w = _cttp_mcut_cord(buf_c, len_w, u3k(qi_quy));
2014-02-27 04:39:48 +04:00
2014-09-11 04:01:32 +04:00
len_w = _cttp_mcut_quay(buf_c, len_w, u3k(t_quy));
2014-02-27 04:39:48 +04:00
}
2014-09-11 04:01:32 +04:00
u3z(quy);
2014-02-27 04:39:48 +04:00
return len_w;
}
2014-03-03 02:31:03 +04:00
/* _cttp_mcut_url(): measure/cut purl, producing relative URL.
2014-02-27 04:39:48 +04:00
*/
static c3_w
2014-09-11 04:01:32 +04:00
_cttp_mcut_url(c3_c* buf_c, c3_w len_w, u3_noun pul)
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
u3_noun q_pul = u3h(u3t(pul));
u3_noun r_pul = u3t(u3t(pul));
2014-03-14 21:47:17 +04:00
2014-02-27 04:39:48 +04:00
len_w = _cttp_mcut_char(buf_c, len_w, '/');
2014-09-11 04:01:32 +04:00
len_w = _cttp_mcut_pork(buf_c, len_w, u3k(q_pul));
2014-02-27 04:39:48 +04:00
2014-09-11 04:01:32 +04:00
if ( u3_nul != r_pul ) {
2014-02-27 04:39:48 +04:00
len_w = _cttp_mcut_char(buf_c, len_w, '?');
2014-09-11 04:01:32 +04:00
len_w = _cttp_mcut_quay(buf_c, len_w, u3k(r_pul));
2014-02-27 04:39:48 +04:00
}
2014-09-11 04:01:32 +04:00
u3z(pul);
2014-02-27 04:39:48 +04:00
return len_w;
}
/* _cttp_creq_port(): stringify port
*/
static c3_c*
_cttp_creq_port(c3_s por_s)
{
c3_c* por_c = c3_malloc(8);
snprintf(por_c, 7, "%d", 0xffff & por_s);
return por_c;
}
2014-02-27 04:39:48 +04:00
/* _cttp_creq_url(): construct url from noun.
*/
static c3_c*
2014-09-11 04:01:32 +04:00
_cttp_creq_url(u3_noun pul)
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
c3_w len_w = _cttp_mcut_url(0, 0, u3k(pul));
2014-04-02 04:47:01 +04:00
c3_c* url_c = c3_malloc(len_w + 1);
2014-02-27 04:39:48 +04:00
_cttp_mcut_url(url_c, 0, pul);
url_c[len_w] = 0;
return url_c;
}
2014-03-03 02:31:03 +04:00
/* _cttp_creq_host(): construct host from noun.
*/
static c3_c*
2014-09-11 04:01:32 +04:00
_cttp_creq_host(u3_noun hot)
2014-03-03 02:31:03 +04:00
{
2014-09-11 04:01:32 +04:00
c3_w len_w = _cttp_mcut_host(0, 0, u3k(hot));
2014-04-02 04:47:01 +04:00
c3_c* hot_c = c3_malloc(len_w + 1);
2014-03-03 02:31:03 +04:00
_cttp_mcut_host(hot_c, 0, hot);
hot_c[len_w] = 0;
return hot_c;
}
/* _cttp_creq_ip(): stringify ip
*/
static c3_c*
_cttp_creq_ip(c3_w ipf_w)
{
c3_c* ipf_c = c3_malloc(17);
snprintf(ipf_c, 16, "%d.%d.%d.%d", (ipf_w >> 24),
((ipf_w >> 16) & 255),
((ipf_w >> 8) & 255),
(ipf_w & 255));
return ipf_c;
}
2014-02-27 04:39:48 +04:00
/* _cttp_httr(): deliver http result.
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_httr(c3_l num_l, c3_w sas_w, u3_noun mes, u3_noun uct)
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
u3_noun htr = u3nt(sas_w, mes, uct);
u3_noun pox = u3nt(u3_blip, c3__http, u3_nul);
2014-02-27 04:39:48 +04:00
2014-11-06 03:20:01 +03:00
u3v_plan(pox, u3nt(c3__they, num_l, htr));
2014-02-27 04:39:48 +04:00
}
/* _cttp_httr_cres(): deliver valid response.
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_httr_cres(c3_l num_l, u3_cres* res_u)
2014-02-27 04:39:48 +04:00
{
2018-03-13 05:13:36 +03:00
_cttp_httr(num_l, res_u->sas_w, res_u-> hed,
( !res_u->bod_u ) ? u3_nul :
u3nc(u3_nul, _cttp_bods_to_octs(res_u->bod_u)));
2014-02-27 04:39:48 +04:00
}
/* _cttp_httr_fail(): fail out a request by number.
*/
static void
_cttp_httr_fail(c3_l num_l, c3_w cod_w, c3_c* msg_c)
2014-02-27 04:39:48 +04:00
{
if ( msg_c ) {
fprintf(stderr, "http: fail (%d, %d): %s\r\n", num_l, cod_w, msg_c);
} else {
fprintf(stderr, "http: fail (%d, %d): %s\r\n", num_l, cod_w, msg_c);
}
return _cttp_httr(num_l, cod_w, u3_nul, u3_nul);
2014-02-27 04:39:48 +04:00
}
2014-09-11 04:01:32 +04:00
/* _cttp_cres_free(): free a u3_cres.
2014-02-27 04:39:48 +04:00
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_cres_free(u3_cres* res_u)
2014-02-27 04:39:48 +04:00
{
_cttp_bods_free(res_u->bod_u);
free(res_u);
}
static void
_cttp_cres_new(u3_creq* ceq_u, c3_w sas_w)
{
ceq_u->res_u = c3_calloc(sizeof(*ceq_u->res_u));
ceq_u->res_u->sas_w = sas_w;
}
/* _cttp_creq_fire_body(): attach body to request buffers.
2014-02-27 04:39:48 +04:00
*/
static void
_cttp_creq_fire_body(u3_creq* ceq_u, u3_hbod *rub_u)
2014-02-27 04:39:48 +04:00
{
c3_assert(!rub_u->nex_u);
if ( !(ceq_u->rub_u) ) {
ceq_u->rub_u = ceq_u->bur_u = rub_u;
2014-02-27 04:39:48 +04:00
}
else {
ceq_u->bur_u->nex_u = rub_u;
ceq_u->bur_u = rub_u;
2014-02-27 04:39:48 +04:00
}
}
/* _cttp_creq_fire_str(): attach string to request buffers.
2014-02-27 04:39:48 +04:00
*/
static void
_cttp_creq_fire_str(u3_creq* ceq_u, c3_c* str_c)
2014-02-27 04:39:48 +04:00
{
_cttp_creq_fire_body(ceq_u, _cttp_bod(strlen(str_c), str_c));
2014-02-27 04:39:48 +04:00
}
/* _cttp_creq_fire_heds(): attach output headers.
2014-02-27 04:39:48 +04:00
*/
static void
_cttp_creq_fire_heds(u3_creq* ceq_u, u3_hhed* hed_u)
2014-02-27 04:39:48 +04:00
{
while ( hed_u ) {
_cttp_creq_fire_body(ceq_u, _cttp_bud(hed_u));
hed_u = hed_u->nex_u;
}
2014-02-27 04:39:48 +04:00
}
/* _cttp_creq_fire(): load request data for into buffers.
2014-02-27 04:39:48 +04:00
*/
static void
_cttp_creq_fire(u3_creq* ceq_u)
2014-02-27 04:39:48 +04:00
{
switch ( ceq_u->met_m ) {
2014-02-27 04:39:48 +04:00
default: c3_assert(0);
case c3__get: _cttp_creq_fire_str(ceq_u, "GET "); break;
case c3__put: _cttp_creq_fire_str(ceq_u, "PUT "); break;
case c3__post: _cttp_creq_fire_str(ceq_u, "POST "); break;
case c3__head: _cttp_creq_fire_str(ceq_u, "HEAD "); break;
case c3__conn: _cttp_creq_fire_str(ceq_u, "CONNECT "); break;
case c3__delt: _cttp_creq_fire_str(ceq_u, "DELETE "); break;
case c3__opts: _cttp_creq_fire_str(ceq_u, "OPTIONS "); break;
case c3__trac: _cttp_creq_fire_str(ceq_u, "TRACE "); break;
}
_cttp_creq_fire_str(ceq_u, ceq_u->url_c);
_cttp_creq_fire_str(ceq_u, " HTTP/1.1\r\n");
{
c3_c* hot_c = ceq_u->hot_c ? ceq_u->hot_c : ceq_u->ipf_c;
c3_c* hos_c;
c3_w len_w;
if ( ceq_u->por_c ) {
len_w = 6 + strlen(hot_c) + 1 + strlen(ceq_u->por_c) + 3;
hos_c = c3_malloc(len_w);
len_w = snprintf(hos_c, len_w, "Host: %s:%s\r\n", hot_c, ceq_u->por_c);
}
else {
len_w = 6 + strlen(hot_c) + 3;
hos_c = c3_malloc(len_w);
len_w = snprintf(hos_c, len_w, "Host: %s\r\n", hot_c);
}
_cttp_creq_fire_body(ceq_u, _cttp_bod(len_w, hos_c));
free(hos_c);
2014-02-27 04:39:48 +04:00
}
_cttp_creq_fire_str(ceq_u, "User-Agent: urbit/vere-" URBIT_VERSION "\r\n");
_cttp_creq_fire_heds(ceq_u, ceq_u->hed_u);
if ( !ceq_u->bod_u ) {
_cttp_creq_fire_body(ceq_u, _cttp_bod(2, "\r\n"));
}
else {
c3_c len_c[41];
c3_w len_w = snprintf(len_c, 40, "Content-Length: %u\r\n",
ceq_u->bod_u->len_w);
_cttp_creq_fire_body(ceq_u, _cttp_bod(len_w, len_c));
_cttp_creq_fire_body(ceq_u, ceq_u->bod_u);
}
}
static u3_creq*
_cttp_creq_find(c3_l num_l)
{
u3_creq* ceq_u = u3_Host.ctp_u.ceq_u;
// XX glories of linear search
//
while ( ceq_u ) {
if ( num_l == ceq_u->num_l ) {
return ceq_u;
}
ceq_u = ceq_u->nex_u;
}
return 0;
}
static void
_cttp_creq_link(u3_creq* ceq_u)
{
ceq_u->nex_u = u3_Host.ctp_u.ceq_u;
u3_Host.ctp_u.ceq_u = ceq_u;
}
static void
_cttp_creq_unlink(u3_creq* ceq_u)
{
if ( ceq_u->pre_u ) {
ceq_u->pre_u->nex_u = ceq_u->nex_u;
2015-05-25 22:14:20 +03:00
}
else {
u3_Host.ctp_u.ceq_u = ceq_u->nex_u;
}
2014-02-27 04:39:48 +04:00
}
/* _cttp_creq_free(): free a u3_creq.
2014-02-27 04:39:48 +04:00
*/
static void
_cttp_creq_free(u3_creq* ceq_u)
2014-02-27 04:39:48 +04:00
{
_cttp_creq_unlink(ceq_u);
2014-02-27 04:39:48 +04:00
_cttp_heds_free(ceq_u->hed_u);
// Note: ceq_u->bod_u is covered here
_cttp_bods_free(ceq_u->rub_u);
2014-02-27 04:39:48 +04:00
if ( ceq_u->res_u ) {
_cttp_cres_free(ceq_u->res_u);
2014-02-27 04:39:48 +04:00
}
free(ceq_u->hot_c);
free(ceq_u->por_c);
free(ceq_u->url_c);
free(ceq_u->vec_u);
free(ceq_u);
2014-02-27 04:39:48 +04:00
}
static void
_cttp_creq_fail(u3_creq* ceq_u, const c3_c* err_c)
{
// XX anything other than a 504?
_cttp_httr_fail(ceq_u->num_l, 504, (c3_c *)err_c);
_cttp_creq_free(ceq_u);
}
static void
_cttp_creq_respond(u3_creq* ceq_u)
{
_cttp_httr_cres(ceq_u->num_l, ceq_u->res_u);
_cttp_creq_free(ceq_u);
}
// req from ++hiss
2014-09-11 04:01:32 +04:00
static u3_creq*
_cttp_creq_new(c3_l num_l, u3_noun hes)
2014-02-27 04:39:48 +04:00
{
u3_creq* ceq_u = c3_calloc(sizeof(*ceq_u));
u3_noun pul = u3h(hes); // ++purl
u3_noun hat = u3h(pul); // ++hart
u3_noun sec = u3h(hat);
u3_noun por = u3h(u3t(hat));
u3_noun hot = u3t(u3t(hat)); // ++host
u3_noun moh = u3t(hes); // ++moth
u3_noun met = u3h(moh); // ++meth
u3_noun mah = u3h(u3t(moh)); // ++math
u3_noun bod = u3t(u3t(moh));
2014-02-27 04:39:48 +04:00
ceq_u->num_l = num_l;
ceq_u->sec = sec;
if ( c3y == u3h(hot) ) {
ceq_u->hot_c = _cttp_creq_host(u3k(u3t(hot)));
} else {
ceq_u->ipf_w = u3r_word(0, u3t(hot));
ceq_u->ipf_c = _cttp_creq_ip(ceq_u->ipf_w);
}
if ( u3_nul != por ) {
ceq_u->por_s = u3t(por);
ceq_u->por_c = _cttp_creq_port(ceq_u->por_s);
}
ceq_u->met_m = met;
2014-09-11 04:01:32 +04:00
ceq_u->url_c = _cttp_creq_url(u3k(pul));
ceq_u->hed_u = _cttp_heds_math(u3k(mah));
2014-02-27 04:39:48 +04:00
if ( u3_nul != bod ) {
ceq_u->bod_u = _cttp_octs_to_bod(u3k(u3t(bod)));
2014-02-27 04:39:48 +04:00
}
_cttp_creq_link(ceq_u);
2014-02-27 04:39:48 +04:00
2014-09-11 04:01:32 +04:00
u3z(hes);
2014-02-27 04:39:48 +04:00
return ceq_u;
}
2014-03-14 21:47:17 +04:00
2014-02-27 04:39:48 +04:00
static void
_cttp_cres_queue_buf(u3_cres* res_u, h2o_buffer_t* buf_u)
2014-02-27 04:39:48 +04:00
{
u3_hbod* bod_u = _cttp_bod(buf_u->size, buf_u->bytes);
if ( !(res_u->bod_u) ) {
res_u->bod_u = res_u->dob_u = bod_u;
2014-02-27 04:39:48 +04:00
}
else {
res_u->dob_u->nex_u = bod_u;
res_u->dob_u = bod_u;
2014-02-27 04:39:48 +04:00
}
}
static h2o_iovec_t*
_cttp_bufs_to_vec(u3_hbod* rub_u, c3_w* tot_w)
2014-03-14 21:47:17 +04:00
{
u3_hbod* bur_u = rub_u;
h2o_iovec_t* vec_u;
c3_w len_w;
2014-02-27 04:39:48 +04:00
len_w = 0;
while( rub_u ) {
len_w++;
rub_u = rub_u->nex_u;
2014-02-27 04:39:48 +04:00
}
vec_u = c3_malloc(sizeof(h2o_iovec_t) * len_w);
rub_u = bur_u;
len_w = 0;
while( rub_u ) {
vec_u[len_w] = h2o_iovec_init(rub_u->hun_y, rub_u->len_w);
len_w++;
rub_u = rub_u->nex_u;
2014-02-27 04:39:48 +04:00
}
*tot_w = len_w;
2014-02-27 04:39:48 +04:00
return vec_u;
2014-03-14 21:47:17 +04:00
}
2014-02-27 04:39:48 +04:00
// XX research: may be called with closed client?
static c3_i
2018-03-12 08:11:18 +03:00
on_body(h2o_http1client_t* cli_u, const c3_c* err_c)
2018-03-12 05:57:16 +03:00
{
u3_creq* ceq_u = (u3_creq *)cli_u->data;
2018-03-12 05:57:16 +03:00
2018-03-12 08:11:18 +03:00
if ( 0 != err_c && h2o_http1client_error_is_eos != err_c ) {
_cttp_creq_fail(ceq_u, err_c);
2018-03-12 05:57:16 +03:00
return -1;
}
h2o_socket_t* sok_u = cli_u->sock;
2018-03-12 08:11:18 +03:00
if ( sok_u->input->size ) {
_cttp_cres_queue_buf(ceq_u->res_u, sok_u->input);
h2o_buffer_consume(&sok_u->input, sok_u->input->size);
2018-03-12 08:11:18 +03:00
}
if ( h2o_http1client_error_is_eos == err_c ) {
_cttp_creq_respond(ceq_u);
2018-03-12 08:11:18 +03:00
}
2018-03-12 05:57:16 +03:00
return 0;
}
// XX deduplicate with _http_vec_to_atom
/* _cttp_vec_to_atom(): convert h2o_iovec_t to atom (cord)
2018-03-13 05:13:36 +03:00
*/
static u3_noun
_cttp_vec_to_atom(h2o_iovec_t vec_u)
2018-03-13 05:13:36 +03:00
{
// XX portable?
return u3i_bytes(vec_u.len, (const c3_y*)vec_u.base);
}
// XX deduplicate with _http_heds_to_noun
/* _cttp_heds_to_noun(): convert h2o_header_t to (list (pair @t @t))
*/
2018-03-13 05:13:36 +03:00
static u3_noun
_cttp_heds_to_noun(h2o_header_t* hed_u, c3_d hed_d)
{
u3_noun hed = u3_nul;
c3_d dex_d = hed_d;
h2o_header_t deh_u;
while ( 0 < dex_d ) {
deh_u = hed_u[--dex_d];
hed = u3nc(u3nc(_cttp_vec_to_atom(*deh_u.name),
_cttp_vec_to_atom(deh_u.value)), hed);
2018-03-13 05:13:36 +03:00
}
return hed;
}
2018-03-12 05:57:16 +03:00
static h2o_http1client_body_cb
2018-03-12 08:11:18 +03:00
on_head(h2o_http1client_t* cli_u, const c3_c* err_c, c3_i ver_i, c3_i sas_i,
h2o_iovec_t sas_u, h2o_header_t* hed_u,
size_t hed_t, c3_i len_i)
2018-03-12 05:57:16 +03:00
{
u3_creq* ceq_u = (u3_creq *)cli_u->data;
2018-03-12 05:57:16 +03:00
2018-03-12 08:11:18 +03:00
if ( 0 != err_c && h2o_http1client_error_is_eos != err_c ) {
_cttp_creq_fail(ceq_u, err_c);
2018-03-12 05:57:16 +03:00
return 0;
}
_cttp_cres_new(ceq_u, (c3_w)sas_i);
ceq_u->res_u->hed = _cttp_heds_to_noun(hed_u, hed_t);
2018-03-12 05:57:16 +03:00
2018-03-12 08:11:18 +03:00
if ( h2o_http1client_error_is_eos == err_c ) {
_cttp_creq_respond(ceq_u);
2018-03-12 05:57:16 +03:00
return 0;
}
return on_body;
}
static h2o_http1client_head_cb
2018-03-12 08:11:18 +03:00
on_connect(h2o_http1client_t* cli_u, const c3_c* err_c, h2o_iovec_t** vec_p,
size_t* vec_t, c3_i* hed_i)
2018-03-12 05:57:16 +03:00
{
u3_creq* ceq_u = (u3_creq *)cli_u->data;
2018-03-12 05:57:16 +03:00
2018-03-12 08:11:18 +03:00
if ( 0 != err_c ) {
_cttp_creq_fail(ceq_u, err_c);
2018-03-12 05:57:16 +03:00
return 0;
}
{
c3_w len_w;
ceq_u->vec_u = _cttp_bufs_to_vec(ceq_u->rub_u, &len_w);
*vec_t = len_w;
*vec_p = ceq_u->vec_u;
*hed_i = c3__head == ceq_u->met_m;
}
2018-03-12 05:57:16 +03:00
return on_head;
}
static void
_cttp_creq_connect(u3_creq* ceq_u)
2018-03-12 05:57:16 +03:00
{
c3_assert(ceq_u->ipf_c);
h2o_iovec_t ipf_u = h2o_iovec_init(ceq_u->ipf_c, strlen(ceq_u->ipf_c));
c3_s por_s = ceq_u->por_s ? ceq_u->por_s :
( c3y == ceq_u->sec ) ? 443 : 80;
2018-03-12 05:57:16 +03:00
// connect by IP
h2o_http1client_connect(&ceq_u->cli_u, ceq_u, u3_Host.ctp_u.ctx_u, ipf_u,
por_s, c3y == ceq_u->sec, on_connect);
2018-03-12 05:57:16 +03:00
// set hostname for TLS handshake
if ( ceq_u->hot_c && c3y == ceq_u->sec ) {
c3_w len_w = 1 + strlen(ceq_u->hot_c);
c3_c* hot_c = c3_malloc(len_w);
strncpy(hot_c, ceq_u->hot_c, len_w);
free(ceq_u->cli_u->ssl.server_name);
ceq_u->cli_u->ssl.server_name = hot_c;
}
2018-03-12 05:57:16 +03:00
_cttp_creq_fire(ceq_u);
2018-03-12 05:57:16 +03:00
}
static void
_cttp_creq_resolve_cb(uv_getaddrinfo_t* adr_u,
c3_i sas_i,
struct addrinfo* aif_u)
{
u3_creq* ceq_u = adr_u->data;
if ( 0 != sas_i ) {
_cttp_creq_fail(ceq_u, uv_strerror(sas_i));
return;
}
ceq_u->ipf_w = ntohl(((struct sockaddr_in *)aif_u->ai_addr)->sin_addr.s_addr);
ceq_u->ipf_c = _cttp_creq_ip(ceq_u->ipf_w);
uv_freeaddrinfo(aif_u);
_cttp_creq_connect(ceq_u);
}
static void
_cttp_creq_resolve(u3_creq* ceq_u)
{
c3_assert(ceq_u->hot_c);
uv_getaddrinfo_t* adr_u = c3_malloc(sizeof(*adr_u));
adr_u->data = ceq_u;
struct addrinfo hin_u;
memset(&hin_u, 0, sizeof(struct addrinfo));
hin_u.ai_family = PF_INET;
hin_u.ai_socktype = SOCK_STREAM;
hin_u.ai_protocol = IPPROTO_TCP;
c3_c* por_c = ceq_u->por_c ? ceq_u->por_c :
( c3y == ceq_u->sec ) ? "443" : "80";
c3_i sas_i;
if ( 0 != (sas_i = uv_getaddrinfo(u3L, adr_u, _cttp_creq_resolve_cb,
ceq_u->hot_c, por_c, &hin_u)) ) {
_cttp_creq_fail(ceq_u, uv_strerror(sas_i));
}
}
static void
_cttp_creq_start(u3_creq* ceq_u)
{
if ( ceq_u->ipf_c ) {
_cttp_creq_connect(ceq_u);
} else {
_cttp_creq_resolve(ceq_u);
}
}
2014-09-11 04:01:32 +04:00
/* u3_cttp_ef_thus(): send %thus effect (outgoing request) to cttp.
2014-02-27 04:39:48 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_cttp_ef_thus(c3_l num_l,
u3_noun cuq)
2014-03-14 21:47:17 +04:00
{
u3_creq* ceq_u;
2014-09-11 04:01:32 +04:00
if ( u3_nul == cuq ) {
ceq_u =_cttp_creq_find(num_l);
if ( ceq_u ) {
h2o_http1client_cancel(ceq_u->cli_u);
_cttp_creq_free(ceq_u);
}
2014-02-27 05:58:40 +04:00
}
else {
2018-03-12 05:57:16 +03:00
#if 1
// ++ hiss {p/purl q/moth}
// ++ purl {p/hart q/pork r/quay}
// ++ hart {p/? q/(unit @ud) r/host}
// ++ host (each (list @t) @if)
// ++ pork {p/(unit @ta) q/(list @t)}
// ++ quay (list {p/@t q/@t})
// ++ moth {p/meth q/math r/(unit octs)}
// ++ math (map @t (list @t))
u3_noun span = u3v_wish("-:!>(*{"
"purl={"
"p/{p/? q/(unit @ud) r/(each (list @t) @if)} "
"q/{p/(unit @ta) q/(list @t)} "
"r/(list {p/@t q/@t})"
"} "
"moth={p/@tas q/(map @t (list @t)) r/(unit octs)}"
"})");
u3m_tape(u3dc("text", span, u3k(u3t(cuq))));
uL(fprintf(uH, "\n"));
#endif
ceq_u = _cttp_creq_new(num_l, u3k(u3t(cuq)));
_cttp_creq_start(ceq_u);
2014-02-27 05:58:40 +04:00
}
2014-09-11 04:01:32 +04:00
u3z(cuq);
2014-02-27 04:39:48 +04:00
}
2014-09-11 04:01:32 +04:00
/* u3_cttp_io_init(): initialize http client I/O.
2014-02-27 04:39:48 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_cttp_io_init()
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
u3_Host.ssl_u = SSL_CTX_new(TLSv1_client_method());
SSL_CTX_set_options(u3S, SSL_OP_NO_SSLv2);
SSL_CTX_set_verify(u3S, SSL_VERIFY_PEER, 0);
2014-09-11 04:01:32 +04:00
SSL_CTX_set_default_verify_paths(u3S);
2015-05-25 22:14:20 +03:00
// if ( 0 == SSL_CTX_load_verify_locations(u3S,
// "/etc/ssl/certs/ca-certificates.crt", NULL) ) {
// fprintf(stderr, "\tload-error\r\n");
// } else {
// fprintf(stderr, "\tload-good\r\n");
// }
2014-09-11 04:01:32 +04:00
SSL_CTX_set_session_cache_mode(u3S, SSL_SESS_CACHE_OFF);
SSL_CTX_set_cipher_list(u3S, "ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:"
2014-03-17 02:53:41 +04:00
"ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:"
"RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS");
2018-03-12 05:57:16 +03:00
h2o_timeout_t* tim_u = c3_malloc(sizeof(*tim_u));
// XX how long? 1 minute?
h2o_timeout_init(u3L, tim_u, 10000);
h2o_http1client_ctx_t* ctx_u = c3_calloc(sizeof(*ctx_u));
ctx_u->loop = u3L;
ctx_u->ssl_ctx = u3S;
ctx_u->io_timeout = tim_u;
u3_Host.ctp_u.ceq_u = 0;
u3_Host.ctp_u.ctx_u = ctx_u;
2014-02-27 04:39:48 +04:00
}
2014-09-11 04:01:32 +04:00
/* u3_cttp_io_poll(): poll kernel for cttp I/O.
2014-02-27 04:39:48 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_cttp_io_poll(void)
2014-02-27 04:39:48 +04:00
{
}
2014-09-11 04:01:32 +04:00
/* u3_cttp_io_exit(): shut down cttp.
2014-02-27 04:39:48 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_cttp_io_exit(void)
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
SSL_CTX_free(u3S);
free(u3_Host.ctp_u.ctx_u->io_timeout);
free(u3_Host.ctp_u.ctx_u);
2014-02-27 04:39:48 +04:00
}