urbit/v/cttp.c

1636 lines
36 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>
#include <openssl/rand.h>
2014-02-27 04:39:48 +04:00
#include "../outside/jhttp/http_parser.h" // Joyent HTTP
#include "all.h"
#include "v/vere.h"
#ifdef U3_OS_osx
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
# pragma clang diagnostic ignored "-Wdeprecated-declarations"
#endif
2014-08-11 07:09:56 +04:00
#define CTTP_NO_PIPELINE
2014-03-14 21:47:17 +04:00
/* Forward declarations.
2014-02-27 04:39:48 +04:00
*/
2014-09-11 04:01:32 +04:00
static void _cttp_ccon_kick(u3_ccon* coc_u);
static void _cttp_ccon_cryp_hurr(u3_ccon* coc_u, c3_i rev_i);
static void _cttp_ccon_cryp_rout(u3_ccon* coc_u);
static void _cttp_ccon_fill(u3_ccon* coc_u);
static void _cttp_ccon_fire(u3_ccon* coc_u, u3_creq* ceq_u);
static void _cttp_ccon_fail_cb(uv_handle_t* wax_u);
2014-09-11 04:01:32 +04:00
static c3_c* _cttp_creq_url(u3_noun pul);
2014-02-27 04:39:48 +04:00
/* _cttp_alloc(): libuv buffer allocator.
*/
2014-08-21 02:09:51 +04:00
static void
_cttp_alloc(uv_handle_t* had_u, size_t len_i, uv_buf_t* buf )
2014-02-27 04:39:48 +04:00
{
2014-08-21 02:09:51 +04:00
void* ptr_v = c3_malloc(len_i);
*buf = uv_buf_init(ptr_v, len_i);
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*
2014-02-27 04:39:48 +04:00
_cttp_bod(c3_w len_w, const c3_y* hun_y)
{
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, hun_y, len_w);
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*
2014-02-27 04:39:48 +04:00
_cttp_bud(c3_c* nam_c, c3_c* val_c)
{
c3_w lnm_w = strlen(nam_c);
c3_w lvl_w = strlen(val_c);
c3_w len_w = lnm_w + 2 + lvl_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
strncpy((c3_c *)bod_u->hun_y, nam_c, lnm_w);
strncpy((c3_c *)bod_u->hun_y + lnm_w, ": ", 2);
strncpy((c3_c *)bod_u->hun_y + lnm_w + 2, val_c, lvl_w);
strncpy((c3_c *)bod_u->hun_y + lnm_w + 2 + lvl_w, "\r\n", 2);
bod_u->len_w = len_w;
bod_u->nex_u = 0;
return bod_u;
}
/* _cttp_heds_to_list(): C headers to list.
*/
2014-09-11 04:01:32 +04:00
static u3_noun
_cttp_heds_to_list(u3_hhed* hed_u)
2014-02-27 04:39:48 +04:00
{
if ( 0 == hed_u ) {
2014-09-11 04:01:32 +04:00
return u3_nul;
2014-02-27 04:39:48 +04:00
} else {
2014-11-06 03:20:01 +03:00
return u3nc(u3nc(u3i_string(hed_u->nam_c),
hed_u->val_c ? u3i_string(hed_u->val_c) : u3_nul),
2014-02-27 04:39:48 +04:00
_cttp_heds_to_list(hed_u->nex_u));
}
}
/* _cttp_heds_free(): free header structure.
*/
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
if ( hed_u->nam_c ) free(hed_u->nam_c);
if ( hed_u->val_c ) free(hed_u->val_c);
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
}
/* _cttp_heds_list(): create headers from list.
*/
2014-09-11 04:01:32 +04:00
static u3_hhed*
_cttp_heds_list(u3_hhed* hed_u, u3_noun nam, u3_noun vaz)
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
u3_noun viz = vaz;
2014-02-27 04:39:48 +04:00
2014-09-11 04:01:32 +04:00
while ( u3_nul != viz ) {
u3_hhed* deh_u;
2014-02-27 04:39:48 +04:00
2014-04-02 04:47:01 +04:00
deh_u = c3_malloc(sizeof(*deh_u));
2014-11-06 03:20:01 +03:00
deh_u->nam_c = u3r_string(nam);
deh_u->val_c = u3r_string(u3h(viz));
2014-02-27 04:39:48 +04:00
deh_u->nex_u = hed_u;
hed_u = deh_u;
2014-09-11 04:01:32 +04:00
viz = u3t(viz);
2014-02-27 04:39:48 +04:00
}
2014-09-11 04:01:32 +04:00
u3z(nam);
u3z(vaz);
2014-02-27 04:39:48 +04:00
return hed_u;
}
/* _cttp_heds_math(): create headers from noun.
*/
2014-09-11 04:01:32 +04:00
static u3_hhed*
_cttp_heds_math(u3_hhed* hed_u, u3_noun mah)
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
if ( u3_nul == mah ) {
2014-02-27 04:39:48 +04:00
return hed_u;
2014-03-14 21:47:17 +04:00
}
2014-02-27 04:39:48 +04:00
else {
2014-09-11 04:01:32 +04:00
u3_noun n_mah = u3h(mah);
u3_noun pn_mah = u3h(n_mah);
u3_noun qn_mah = u3t(n_mah);
u3_noun l_mah = u3h(u3t(mah));
u3_noun r_mah = u3t(u3t(mah));
2014-02-27 04:39:48 +04:00
2014-09-11 04:01:32 +04:00
hed_u = _cttp_heds_list(hed_u, u3k(pn_mah), u3k(qn_mah));
hed_u = _cttp_heds_math(hed_u, u3k(l_mah));
hed_u = _cttp_heds_math(hed_u, u3k(r_mah));
2014-02-27 04:39:48 +04:00
2014-09-11 04:01:32 +04:00
u3z(mah);
2014-02-27 04:39:48 +04:00
return hed_u;
}
}
/* _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_str(): measure/cut string.
*/
static c3_w
_cttp_mcut_str(c3_c* buf_c, c3_w len_w, const c3_c* str_c)
{
c3_w str_w = strlen(str_c);
if ( buf_c ) {
strncpy(buf_c + len_w, str_c, str_w);
}
return (len_w + str_w);
}
/* _cttp_mcut_span(): measure/cut span.
*/
static c3_w
2014-09-11 04:01:32 +04:00
_cttp_mcut_span(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 span list.
*/
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
2014-09-11 04:01:32 +04:00
len_w = _cttp_mcut_span(buf_c, len_w, u3k(h_axp));
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
{
2014-11-05 04:18:47 +03:00
if ( c3y == u3h(hot) ) {
2014-11-06 22:13:57 +03:00
len_w = _cttp_mcut_path(buf_c, len_w, '.', u3kb_flop(u3k(u3t(hot))));
2014-02-27 04:39:48 +04:00
}
else {
2014-11-06 03:20:01 +03:00
c3_w ipf_w = u3r_word(0, u3t(hot));
2014-02-27 04:39:48 +04:00
c3_c ipf_c[17];
snprintf(ipf_c, 16, "%d.%d.%d.%d", (ipf_w >> 24),
((ipf_w >> 16) & 255),
((ipf_w >> 8) & 255),
(ipf_w & 255));
len_w = _cttp_mcut_str(buf_c, len_w, ipf_c);
}
2014-09-11 04:01:32 +04:00
u3z(hot);
2014-02-27 04:39:48 +04:00
return len_w;
}
2014-03-03 02:31:03 +04:00
#if 0
2014-02-27 04:39:48 +04:00
/* _cttp_mcut_pfix(): measure/cut prefix.
*/
static c3_w
2014-09-11 04:01:32 +04:00
_cttp_mcut_pfix(c3_c* buf_c, c3_w len_w, u3_noun hat)
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
u3_noun p_hat = u3h(hat);
u3_noun q_hat = u3h(u3t(hat));
u3_noun r_hat = u3t(u3t(hat));
2014-02-27 04:39:48 +04:00
2014-11-05 04:18:47 +03:00
if ( c3y == p_hat ) {
2014-02-27 04:39:48 +04:00
len_w = _cttp_mcut_str(buf_c, len_w, "https://");
} else {
2014-02-27 05:58:40 +04:00
len_w = _cttp_mcut_str(buf_c, len_w, "http://");
2014-02-27 04:39:48 +04:00
}
2014-09-11 04:01:32 +04:00
len_w = _cttp_mcut_host(buf_c, len_w, u3k(r_hat));
2014-02-27 04:39:48 +04:00
2014-09-11 04:01:32 +04:00
if ( u3_nul != q_hat ) {
2014-11-06 03:20:01 +03:00
c3_w por_w = 0xffff & u3r_word(0, u3t(q_hat));
2014-02-27 04:39:48 +04:00
c3_c por_c[8];
snprintf(por_c, 7, ":%d", por_w);
len_w = _cttp_mcut_str(buf_c, len_w, por_c);
}
2014-09-11 04:01:32 +04:00
u3z(hat);
2014-02-27 04:39:48 +04:00
return len_w;
}
2014-03-03 02:31:03 +04:00
#endif
2014-02-27 04:39:48 +04:00
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, '.');
2014-09-11 04:01:32 +04:00
len_w = _cttp_mcut_span(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, '&');
2014-09-11 04:01:32 +04:00
len_w = _cttp_mcut_span(buf_c, len_w, u3k(pi_quy));
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_span(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-09-11 04:01:32 +04:00
// len_w = _cttp_mcut_pfix(buf_c, len_w, u3k(p_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_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_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;
}
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
{
_cttp_httr
(num_l,
res_u->sas_w,
_cttp_heds_to_list(res_u->hed_u),
2014-09-11 04:01:32 +04:00
res_u->bod_u ? u3nc(u3_nul, _cttp_bods_to_octs(res_u->bod_u)) : u3_nul);
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_c* msg_c)
{
2014-09-11 04:01:32 +04:00
return _cttp_httr(num_l, 404, 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_heds_free(res_u->hed_u);
_cttp_bods_free(res_u->bod_u);
free(res_u->par_u);
free(res_u);
}
2014-09-11 04:01:32 +04:00
/* _cttp_creq_free(): free a u3_creq.
2014-02-27 04:39:48 +04:00
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_creq_free(u3_creq* ceq_u)
2014-02-27 04:39:48 +04:00
{
_cttp_heds_free(ceq_u->hed_u);
_cttp_bods_free(ceq_u->bod_u);
if ( ceq_u->res_u ) {
_cttp_cres_free(ceq_u->res_u);
}
free(ceq_u->url_c);
free(ceq_u);
}
/* _cttp_message_begin(): jhttp callback
*/
static c3_i
_cttp_message_begin(http_parser* par_u)
{
return 0;
}
/* _cttp_more(): extend string with new data.
*/
static c3_c*
_cttp_more(c3_c* str_c, const c3_c* buf_c, size_t siz_i)
{
if ( !str_c ) {
2014-04-02 04:47:01 +04:00
str_c = c3_malloc(siz_i + 1);
2014-02-27 04:39:48 +04:00
memcpy(str_c, buf_c, siz_i);
str_c[siz_i] = 0;
}
else {
c3_w len_w = strlen(str_c);
str_c = realloc(str_c, len_w + siz_i + 1);
memcpy(str_c + len_w, buf_c, siz_i);
str_c[len_w + siz_i] = 0;
}
return str_c;
}
/* _cttp_url(): jhttp callback
*/
static c3_i
_cttp_url(http_parser* par_u, const c3_c* buf_c, size_t siz_i)
{
c3_assert(!"what?");
return 0;
}
/* _cttp_header_field(): jhttp callback
*/
static c3_i
_cttp_header_field(http_parser* par_u, const c3_c* buf_c, size_t siz_i)
{
2014-09-11 04:01:32 +04:00
u3_creq* ceq_u = par_u->data;
u3_cres* res_u = ceq_u->res_u;
2014-02-27 04:39:48 +04:00
switch ( res_u->rat_e ) {
2014-09-11 04:01:32 +04:00
case u3_hreq_non:
case u3_hreq_val: {
u3_hhed* hed_u = c3_malloc(sizeof(*hed_u));
2014-02-27 04:39:48 +04:00
hed_u->nam_c = _cttp_more(0, buf_c, siz_i);
hed_u->val_c = 0;
hed_u->nex_u = res_u->hed_u;
res_u->hed_u = hed_u;
break;
}
2014-09-11 04:01:32 +04:00
case u3_hreq_nam: {
2014-02-27 04:39:48 +04:00
res_u->hed_u->nam_c = _cttp_more(res_u->hed_u->nam_c, buf_c, siz_i);
break;
}
}
2014-09-11 04:01:32 +04:00
res_u->rat_e = u3_hreq_nam;
2014-02-27 04:39:48 +04:00
return 0;
}
/* _cttp_header_value(): jhttp callback
*/
static c3_i
_cttp_header_value(http_parser* par_u, const c3_c* buf_c, size_t siz_i)
{
2014-09-11 04:01:32 +04:00
u3_creq* ceq_u = par_u->data;
u3_cres* res_u = ceq_u->res_u;
2014-02-27 04:39:48 +04:00
switch ( res_u->rat_e ) {
2014-09-11 04:01:32 +04:00
case u3_hreq_non: fprintf(stderr, "http: odd value\r\n"); return 1;
2014-02-27 04:39:48 +04:00
2014-09-11 04:01:32 +04:00
case u3_hreq_nam: {
2014-02-27 04:39:48 +04:00
res_u->hed_u->val_c = _cttp_more(0, buf_c, siz_i);
break;
}
2014-09-11 04:01:32 +04:00
case u3_hreq_val: {
2014-02-27 04:39:48 +04:00
res_u->hed_u->val_c = _cttp_more(res_u->hed_u->val_c, buf_c, siz_i);
break;
}
}
2014-09-11 04:01:32 +04:00
res_u->rat_e = u3_hreq_val;
2014-02-27 04:39:48 +04:00
return 0;
}
/* _cttp_headers_complete(): jhttp callback
*/
static c3_i
_cttp_headers_complete(http_parser* par_u)
{
return 0;
}
/* _cttp_body(): jhttp callback
*/
static c3_i
_cttp_body(http_parser* par_u, const c3_c* buf_c, size_t siz_i)
{
2014-09-11 04:01:32 +04:00
u3_creq* ceq_u = par_u->data;
u3_cres* res_u = ceq_u->res_u;
u3_hbod* bod_u;
2014-02-27 04:39:48 +04:00
2014-03-03 23:51:35 +04:00
bod_u = _cttp_bod(siz_i, (const c3_y*)buf_c);
2014-02-27 04:39:48 +04:00
2014-03-03 23:51:35 +04:00
if ( !(res_u->bod_u) ) {
res_u->bod_u = res_u->dob_u = bod_u;
2014-02-27 04:39:48 +04:00
}
2014-03-03 23:51:35 +04:00
else {
res_u->dob_u->nex_u = bod_u;
res_u->dob_u = bod_u;
}
return 0;
2014-02-27 04:39:48 +04:00
}
/* _cttp_message_complete(): jhttp callback
*/
static c3_i
_cttp_message_complete(http_parser* par_u)
{
2014-09-11 04:01:32 +04:00
u3_creq* ceq_u = par_u->data;
u3_ccon* coc_u = ceq_u->coc_u;
u3_cres* res_u = ceq_u->res_u;
2014-02-27 04:39:48 +04:00
res_u->sas_w = par_u->status_code;
// Send response to the event system.
//
2014-03-03 02:31:03 +04:00
// uL(fprintf(uH, "response for %s: %d\n", ceq_u->url_c, res_u->sas_w));
2014-02-27 04:39:48 +04:00
_cttp_httr_cres(ceq_u->num_l, res_u);
_cttp_cres_free(res_u);
ceq_u->res_u = 0;
coc_u->ceq_u = ceq_u->nex_u;
if ( 0 == coc_u->ceq_u ) {
c3_assert(ceq_u == coc_u->qec_u);
coc_u->qec_u = 0;
}
2014-11-05 04:18:47 +03:00
if ( c3y == coc_u->sec ) {
SSL_shutdown(coc_u->ssl.ssl_u);
_cttp_ccon_cryp_rout(coc_u);
2014-08-11 11:47:46 +04:00
// uL(fprintf(uH, "cttp: close b: %p\n", coc_u));
uv_close((uv_handle_t*)&coc_u->wax_u, _cttp_ccon_fail_cb);
}
2014-02-27 04:39:48 +04:00
return 0;
}
/* _cttp_settings[]: callback array.
*/
static struct http_parser_settings _cttp_settings = {
_cttp_message_begin,
_cttp_url,
_cttp_header_field,
_cttp_header_value,
_cttp_headers_complete,
_cttp_body,
_cttp_message_complete
};
/* _cttp_cres_start(): set up new cttp response for creq.
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_cres_start(u3_creq* ceq_u)
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
u3_cres* res_u = c3_malloc(sizeof(*res_u));
2014-02-27 04:39:48 +04:00
memset(res_u, 0, sizeof(*res_u));
ceq_u->res_u = res_u;
2014-04-02 04:47:01 +04:00
res_u->par_u = c3_malloc(sizeof(struct http_parser));
2014-02-27 04:39:48 +04:00
http_parser_init(res_u->par_u, HTTP_RESPONSE);
((struct http_parser *)(res_u->par_u))->data = ceq_u;
}
/* _cttp_ccon_wax(): connection from wax_u.
*/
2014-09-11 04:01:32 +04:00
static u3_ccon*
2014-02-27 04:39:48 +04:00
_cttp_ccon_wax(uv_tcp_t* wax_u)
{
2014-09-11 04:01:32 +04:00
u3_ccon* coc_u = 0;
2014-02-27 04:39:48 +04:00
2014-09-11 04:01:32 +04:00
return (u3_ccon*)(void *)
2014-03-14 21:47:17 +04:00
( ((c3_y *)(void *)wax_u) -
2014-03-03 02:31:03 +04:00
(((c3_y *)(void *)&(coc_u->wax_u)) - ((c3_y *)(void *)(coc_u))) );
2014-02-27 04:39:48 +04:00
}
/* _cttp_ccon_cot(): connection from cot_u.
*/
2014-09-11 04:01:32 +04:00
static u3_ccon*
2014-02-27 04:39:48 +04:00
_cttp_ccon_cot(uv_connect_t* cot_u)
{
2014-09-11 04:01:32 +04:00
u3_ccon* coc_u = 0;
2014-02-27 04:39:48 +04:00
2014-09-11 04:01:32 +04:00
return (u3_ccon*)(void *)
2014-03-14 21:47:17 +04:00
( ((c3_y *)(void *)cot_u) -
2014-03-03 02:31:03 +04:00
(((c3_y *)(void *)&(coc_u->cot_u)) - ((c3_y *)(void *)(coc_u))) );
2014-02-27 04:39:48 +04:00
}
/* _cttp_ccon_adr(): connection from adr_u.
*/
2014-09-11 04:01:32 +04:00
static u3_ccon*
2014-02-27 04:39:48 +04:00
_cttp_ccon_adr(uv_getaddrinfo_t* adr_u)
{
2014-09-11 04:01:32 +04:00
u3_ccon* coc_u = 0;
2014-02-27 04:39:48 +04:00
2014-09-11 04:01:32 +04:00
return (u3_ccon*)(void *)
2014-03-14 21:47:17 +04:00
( ((c3_y *)(void *)adr_u) -
2014-03-03 02:31:03 +04:00
(((c3_y *)(void *)&(coc_u->adr_u)) - ((c3_y *)(void *)(coc_u))) );
2014-02-27 04:39:48 +04:00
}
/* _cttp_ccon_waste(): fail out whole connection, with message.
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_ccon_waste(u3_ccon* coc_u, c3_c* msg_c)
2014-02-27 04:39:48 +04:00
{
while ( coc_u->ceq_u ) {
2014-09-11 04:01:32 +04:00
u3_creq* ceq_u = coc_u->ceq_u;
2014-02-27 04:39:48 +04:00
_cttp_httr_fail(ceq_u->num_l, msg_c);
coc_u->ceq_u = ceq_u->nex_u;
if ( 0 == coc_u->ceq_u ) {
c3_assert(ceq_u == coc_u->qec_u);
coc_u->qec_u = 0;
}
_cttp_creq_free(ceq_u);
}
2014-03-14 21:47:17 +04:00
2014-03-03 02:31:03 +04:00
free(coc_u->hot_c);
2014-02-27 04:39:48 +04:00
_cttp_bods_free(coc_u->rub_u);
if ( coc_u->pre_u ) {
coc_u->pre_u->nex_u = coc_u->nex_u;
} else {
2014-09-11 04:01:32 +04:00
u3_Host.ctp_u.coc_u = coc_u->nex_u;
2014-02-27 04:39:48 +04:00
}
if ( coc_u->nex_u ) {
coc_u->nex_u->pre_u = coc_u->pre_u;
}
if ( coc_u->ssl.ssl_u ) {
2014-05-15 23:55:30 +04:00
SSL_free(coc_u->ssl.ssl_u);
coc_u->ssl.ssl_u = 0;
coc_u->ssl.rio_u = 0;
coc_u->ssl.wio_u = 0;
}
2014-02-27 04:39:48 +04:00
free(coc_u);
}
/* _cttp_ccon_reset(): reset a live connection.
*/
void
2014-09-11 04:01:32 +04:00
_cttp_ccon_reset(u3_ccon* coc_u)
2014-02-27 04:39:48 +04:00
{
if ( coc_u->ceq_u ) {
_cttp_bods_free(coc_u->rub_u);
coc_u->rub_u = coc_u->bur_u = 0;
if ( coc_u->ceq_u->res_u ) {
_cttp_cres_free(coc_u->ceq_u->res_u);
coc_u->ceq_u->res_u = 0;
}
}
else {
c3_assert(0 == coc_u->rub_u);
}
}
/* _cttp_ccon_reboot(): stop appropriate I/O on failure case.
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_ccon_reboot(u3_ccon* coc_u)
2014-02-27 04:39:48 +04:00
{
switch ( coc_u->sat_e ) {
default: c3_assert(0);
2014-09-11 04:01:32 +04:00
case u3_csat_dead: {
2014-02-27 04:39:48 +04:00
/* Failed to resolve an address. Waste it.
*/
_cttp_ccon_waste(coc_u, "could not resolve address");
break;
}
2014-09-11 04:01:32 +04:00
case u3_csat_addr: {
2014-02-27 04:39:48 +04:00
/* Got an address but not a connection. Waste it.
*/
_cttp_ccon_waste(coc_u, "connection failed");
break;
}
2014-09-11 04:01:32 +04:00
case u3_csat_crop:
case u3_csat_sing: {
/* Got a connection, but SSL failed. Waste it.
*/
_cttp_ccon_waste(coc_u, "ssl handshake failed");
break;
}
2014-09-11 04:01:32 +04:00
case u3_csat_cryp:
case u3_csat_clyr: {
2014-03-14 21:47:17 +04:00
/* We had a connection but it broke. Either there are no
2014-03-03 02:31:03 +04:00
** living requests, in which case waste; otherwise reset.
2014-02-27 04:39:48 +04:00
*/
2014-03-03 02:31:03 +04:00
if ( 0 == coc_u->ceq_u ) {
_cttp_ccon_waste(coc_u, 0);
}
else {
/* Clear any unsent data.
*/
2014-09-11 04:01:32 +04:00
coc_u->sat_e = u3_csat_dead;
2014-03-03 02:31:03 +04:00
_cttp_ccon_reset(coc_u);
/* Begin again.
*/
2014-08-11 07:09:56 +04:00
uL(fprintf(uH, "ccon: rekick\r\n"));
2014-03-03 02:31:03 +04:00
_cttp_ccon_kick(coc_u);
}
2014-02-27 04:39:48 +04:00
break;
}
}
}
/* _cttp_ccon_fail_cb(): complete failure.
*/
static void
_cttp_ccon_fail_cb(uv_handle_t* wax_u)
{
2014-09-11 04:01:32 +04:00
u3_ccon *coc_u = _cttp_ccon_wax((uv_tcp_t*)wax_u);
2014-02-27 04:39:48 +04:00
_cttp_ccon_reboot(coc_u);
}
/* _cttp_ccon_fail(): report failure and reset connection.
*/
static void
2014-11-06 06:10:22 +03:00
_cttp_ccon_fail(u3_ccon* coc_u, u3_noun say)
2014-02-27 04:39:48 +04:00
{
2014-11-05 04:18:47 +03:00
if ( c3y == say ) {
2014-08-21 02:09:51 +04:00
uL(fprintf(uH, "cttp: ERROR\n"));
2014-02-27 04:39:48 +04:00
}
2014-09-11 04:01:32 +04:00
if ( coc_u->sat_e < u3_csat_crop ) {
2014-02-27 04:39:48 +04:00
_cttp_ccon_reboot(coc_u);
}
else {
2014-08-11 07:09:56 +04:00
uL(fprintf(uH, "cttp: close: %p\n", coc_u));
2014-02-27 04:39:48 +04:00
uv_close((uv_handle_t*)&coc_u->wax_u, _cttp_ccon_fail_cb);
}
}
/* _cttp_ccon_kick_resolve_cb(): complete address resolution.
*/
static void
_cttp_ccon_kick_resolve_cb(uv_getaddrinfo_t* adr_u,
c3_i sas_i,
struct addrinfo* aif_u)
{
2014-09-11 04:01:32 +04:00
u3_ccon* coc_u = _cttp_ccon_adr(adr_u);
2014-02-27 04:39:48 +04:00
2014-09-11 04:01:32 +04:00
c3_assert(u3_csat_dead == coc_u->sat_e);
2014-02-27 04:39:48 +04:00
if ( 0 != sas_i ) {
2014-11-05 04:18:47 +03:00
_cttp_ccon_fail(coc_u, c3y);
2014-02-27 04:39:48 +04:00
}
else {
2014-03-03 02:31:03 +04:00
coc_u->ipf_w = ntohl(((struct sockaddr_in *)aif_u->ai_addr)->
sin_addr.s_addr);
2014-09-11 04:01:32 +04:00
coc_u->sat_e = u3_csat_addr;
2014-03-03 02:31:03 +04:00
2014-02-27 04:39:48 +04:00
_cttp_ccon_kick(coc_u);
}
}
/* _cttp_ccon_kick_resolve(): start address resolution.
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_ccon_kick_resolve(u3_ccon* coc_u)
2014-02-27 04:39:48 +04:00
{
c3_c por_c[8];
struct addrinfo hin_u;
2014-09-11 04:01:32 +04:00
c3_assert(u3_csat_dead == coc_u->sat_e);
2014-02-27 04:39:48 +04:00
2014-03-14 21:47:17 +04:00
snprintf(por_c, 7, "%d", 65535 & coc_u->por_s);
2014-02-27 04:39:48 +04:00
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;
2014-09-11 04:01:32 +04:00
if ( 0 != uv_getaddrinfo(u3L, &coc_u->adr_u,
2014-03-14 21:47:17 +04:00
_cttp_ccon_kick_resolve_cb,
2014-03-03 02:31:03 +04:00
coc_u->hot_c, por_c, &hin_u) )
2014-02-27 04:39:48 +04:00
{
2014-11-05 04:18:47 +03:00
_cttp_ccon_fail(coc_u, c3y);
2014-02-27 04:39:48 +04:00
}
}
/* _cttp_ccon_kick_connect_cb(): connect callback.
*/
static void
_cttp_ccon_kick_connect_cb(uv_connect_t* cot_u,
c3_i sas_i)
{
2014-09-11 04:01:32 +04:00
u3_ccon* coc_u = _cttp_ccon_cot(cot_u);
2014-02-27 04:39:48 +04:00
2014-09-11 04:01:32 +04:00
c3_assert(u3_csat_addr == coc_u->sat_e);
2014-02-27 04:39:48 +04:00
if ( 0 != sas_i ) {
2014-11-05 04:18:47 +03:00
_cttp_ccon_fail(coc_u, c3y);
2014-02-27 04:39:48 +04:00
}
else {
2014-11-05 04:18:47 +03:00
coc_u->sat_e = (c3y == coc_u->sec) ?
2014-09-11 04:01:32 +04:00
u3_csat_crop :
u3_csat_clyr;
2014-02-27 04:39:48 +04:00
_cttp_ccon_kick(coc_u);
}
}
/* _cttp_ccon_kick_connect(): start connect resolution.
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_ccon_kick_connect(u3_ccon* coc_u)
2014-02-27 04:39:48 +04:00
{
struct sockaddr_in add_u;
2014-09-11 04:01:32 +04:00
c3_assert(u3_csat_addr == coc_u->sat_e);
2014-02-27 04:39:48 +04:00
2014-09-11 04:01:32 +04:00
if ( 0 != uv_tcp_init(u3L, &coc_u->wax_u) ) {
2014-11-05 04:18:47 +03:00
_cttp_ccon_fail(coc_u, c3y);
2014-02-27 04:39:48 +04:00
}
add_u.sin_family = AF_INET;
add_u.sin_port = htons(coc_u->por_s);
add_u.sin_addr.s_addr = htonl(coc_u->ipf_w);
2014-03-14 21:47:17 +04:00
if ( 0 != uv_tcp_connect(&coc_u->cot_u,
&coc_u->wax_u,
2014-08-21 02:09:51 +04:00
(const struct sockaddr*) & add_u,
2014-02-27 04:39:48 +04:00
_cttp_ccon_kick_connect_cb) )
{
2014-11-05 04:18:47 +03:00
_cttp_ccon_fail(coc_u, c3y);
2014-02-27 04:39:48 +04:00
}
}
/* An unusual lameness in libuv.
*/
typedef struct {
uv_write_t wri_u;
2014-09-11 04:01:32 +04:00
u3_ccon* coc_u;
2014-02-27 04:39:48 +04:00
c3_y* buf_y;
2014-09-11 04:01:32 +04:00
} _u3_write_t;
2014-02-27 04:39:48 +04:00
/* _cttp_ccon_kick_write_cb(): general write callback
2014-02-27 04:39:48 +04:00
*/
static void
_cttp_ccon_kick_write_cb(uv_write_t* wri_u, c3_i sas_i)
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
u3_lo_open();
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
_u3_write_t* ruq_u = (void *)wri_u;
2014-02-27 04:39:48 +04:00
if ( 0 != sas_i ) {
2014-11-05 04:18:47 +03:00
_cttp_ccon_fail(ruq_u->coc_u, c3y);
2014-02-27 04:39:48 +04:00
}
free(ruq_u->buf_y);
free(ruq_u);
}
2014-11-05 04:18:47 +03:00
u3_lo_shut(c3n);
2014-02-27 04:39:48 +04:00
}
/* _cttp_ccon_kick_write()
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_ccon_kick_write_cryp(u3_ccon* coc_u)
{
if (!SSL_is_init_finished(coc_u->ssl.ssl_u)) {
return;
}
while ( coc_u->rub_u ) {
2014-09-11 04:01:32 +04:00
u3_hbod* rub_u = coc_u->rub_u;
c3_i rev_i;
coc_u->rub_u = coc_u->rub_u->nex_u;
if ( 0 == coc_u->rub_u ) {
c3_assert(rub_u == coc_u->bur_u);
coc_u->bur_u = 0;
}
if ( 0 >
(rev_i = SSL_write(coc_u->ssl.ssl_u, rub_u->hun_y, rub_u->len_w)) ) {
_cttp_ccon_cryp_hurr(coc_u, rev_i);
_cttp_ccon_cryp_rout(coc_u);
}
}
}
2014-02-27 04:39:48 +04:00
/* _cttp_ccon_kick_write_buf(): transmit buffer.
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_ccon_kick_write_buf(u3_ccon* coc_u, uv_buf_t buf_u)
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
_u3_write_t* ruq_u = (_u3_write_t*) c3_malloc(sizeof(_u3_write_t));
2014-02-27 04:39:48 +04:00
ruq_u->coc_u = coc_u;
ruq_u->buf_y = (c3_y*)buf_u.base;
if ( 0 != uv_write(&ruq_u->wri_u,
(uv_stream_t*)&(coc_u->wax_u),
&buf_u, 1,
_cttp_ccon_kick_write_cb) )
2014-02-27 04:39:48 +04:00
{
2014-11-05 04:18:47 +03:00
_cttp_ccon_fail(coc_u, c3y);
2014-02-27 04:39:48 +04:00
}
}
/* _cttp_ccon_kick_write_body(): attach response body.
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_ccon_kick_write_body(u3_ccon* coc_u, u3_hbod *rub_u)
2014-02-27 04:39:48 +04:00
{
uv_buf_t buf_u;
// XX extra copy here due to old code. Use hbod as base directly.
//
{
2014-04-02 04:47:01 +04:00
c3_y* buf_y = c3_malloc(rub_u->len_w + 1);
2014-02-27 04:39:48 +04:00
memcpy(buf_y, rub_u->hun_y, rub_u->len_w);
2014-03-03 02:31:03 +04:00
buf_y[rub_u->len_w] = 0;
// write(1, buf_y, rub_u->len_w);
2014-02-27 04:39:48 +04:00
buf_u = uv_buf_init((c3_c*)buf_y, rub_u->len_w);
}
_cttp_ccon_kick_write_buf(coc_u, buf_u);
}
/* _cttp_ccon_kick_write()
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_ccon_kick_write(u3_ccon* coc_u)
2014-02-27 04:39:48 +04:00
{
while ( coc_u->rub_u ) {
2014-09-11 04:01:32 +04:00
u3_hbod* rub_u = coc_u->rub_u;
2014-02-27 04:39:48 +04:00
coc_u->rub_u = coc_u->rub_u->nex_u;
if ( 0 == coc_u->rub_u ) {
c3_assert(rub_u == coc_u->bur_u);
coc_u->bur_u = 0;
}
_cttp_ccon_kick_write_body(coc_u, rub_u);
}
}
/* _cttp_ccon_cryp_rout: write the SSL buffer to the network
2014-03-14 21:24:09 +04:00
*/
2014-02-27 04:39:48 +04:00
static void
2014-09-11 04:01:32 +04:00
_cttp_ccon_cryp_rout(u3_ccon* coc_u)
{
2014-03-14 21:24:09 +04:00
uv_buf_t buf_u;
c3_i bur_i;
{
2014-04-02 04:47:01 +04:00
c3_y* buf_y = c3_malloc(1<<14);
2014-03-14 21:24:09 +04:00
while ( 0 < (bur_i = BIO_read(coc_u->ssl.wio_u, buf_y, 1<<14)) ) {
buf_u = uv_buf_init((c3_c*)buf_y, bur_i);
_cttp_ccon_kick_write_buf(coc_u, buf_u);
}
}
2014-03-14 21:24:09 +04:00
}
/* _cttp_ccon_cryp_hurr: handle SSL errors
2014-03-14 21:24:09 +04:00
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_ccon_cryp_hurr(u3_ccon* coc_u, int rev)
2014-03-14 21:24:09 +04:00
{
2014-09-11 04:01:32 +04:00
u3_sslx* ssl = &coc_u->ssl;
2014-03-14 21:24:09 +04:00
c3_i err = SSL_get_error(ssl->ssl_u, rev);
switch ( err ) {
default:
_cttp_ccon_waste(coc_u, "ssl lost");
break;
case SSL_ERROR_NONE:
2014-03-17 02:53:41 +04:00
case SSL_ERROR_ZERO_RETURN:
break;
2014-03-14 21:24:09 +04:00
case SSL_ERROR_WANT_WRITE: // XX maybe bad
break;
case SSL_ERROR_WANT_READ:
_cttp_ccon_cryp_rout(coc_u);
2014-03-14 21:24:09 +04:00
break;
}
}
2014-03-14 21:24:09 +04:00
/* _cttp_ccon_pars_shov: shove a data buffer into the parser
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_ccon_pars_shov(u3_ccon* coc_u, void* buf_u, ssize_t siz_i)
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
u3_creq* ceq_u = coc_u->ceq_u;
if ( !ceq_u ) { // spurious input
uL(fprintf(uH, "http: response to no request\n"));
}
else {
if ( !ceq_u->res_u ) {
_cttp_cres_start(ceq_u);
}
if ( siz_i != http_parser_execute(ceq_u->res_u->par_u,
&_cttp_settings,
(c3_c*)buf_u,
siz_i) )
{
uL(fprintf(uH, "http: parse error\n"));
2014-11-05 04:18:47 +03:00
_cttp_ccon_fail(coc_u, c3n);
}
}
}
/* _cttp_ccon_cryp_pull(): pull cleartext data off of the SSL buffer
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_ccon_cryp_pull(u3_ccon* coc_u)
{
if ( SSL_is_init_finished(coc_u->ssl.ssl_u) ) {
static c3_c buf[1<<14];
c3_i ruf;
while ( 0 < (ruf = SSL_read(coc_u->ssl.ssl_u, &buf, sizeof(buf))) ) {
_cttp_ccon_pars_shov(coc_u, &buf, ruf);
}
2014-03-17 02:53:41 +04:00
if ( 0 >= ruf ) {
2014-03-15 20:21:50 +04:00
_cttp_ccon_cryp_hurr(coc_u, ruf);
}
}
else {
// not connected
c3_i r = SSL_connect(coc_u->ssl.ssl_u);
if ( 0 > r ) {
_cttp_ccon_cryp_hurr(coc_u, r);
}
else {
2014-09-11 04:01:32 +04:00
coc_u->sat_e = u3_csat_cryp;
_cttp_ccon_kick(coc_u);
}
}
_cttp_ccon_kick_write_cryp(coc_u);
}
2014-08-23 01:33:27 +04:00
/*
* `nread` (siz_w) is > 0 if there is data available, 0 if libuv is done reading for
* now, or < 0 on error.
*
* The callee is responsible for closing the stream when an error happens
* by calling uv_close(). Trying to read from the stream again is undefined.
*
* The callee is responsible for freeing the buffer, libuv does not reuse it.
* The buffer may be a null buffer (where buf->base=NULL and buf->len=0) on
* error.
*/
static void
_cttp_ccon_kick_read_cryp_cb(uv_stream_t* tcp_u,
2014-08-23 01:33:27 +04:00
ssize_t siz_w,
const uv_buf_t * buf_u)
{
2014-09-11 04:01:32 +04:00
u3_ccon *coc_u = _cttp_ccon_wax((uv_tcp_t*)tcp_u);
2014-02-27 04:39:48 +04:00
2014-09-11 04:01:32 +04:00
u3_lo_open();
2014-02-27 04:39:48 +04:00
{
2014-08-23 01:33:27 +04:00
if ( siz_w == UV_EOF ) {
2014-11-05 04:18:47 +03:00
_cttp_ccon_fail(coc_u, c3n);
2014-08-23 01:33:27 +04:00
} else if ( siz_w < 0 ) {
uL(fprintf(uH, "cttp: read 2: %s\n", uv_strerror(siz_w)));
2014-11-05 04:18:47 +03:00
_cttp_ccon_fail(coc_u, c3y);
2014-02-27 04:39:48 +04:00
}
else {
2014-09-11 04:01:32 +04:00
u3_creq* ceq_u = coc_u->ceq_u;
2014-02-27 04:39:48 +04:00
if ( !ceq_u ) { // spurious input
2014-03-03 02:31:03 +04:00
uL(fprintf(uH, "http: response to no request\n"));
2014-02-27 04:39:48 +04:00
}
2014-03-03 02:31:03 +04:00
else {
2014-08-23 01:33:27 +04:00
BIO_write(coc_u->ssl.rio_u, (c3_c*)buf_u->base, siz_w);
_cttp_ccon_cryp_pull(coc_u);
2014-02-27 04:39:48 +04:00
}
}
2014-08-23 01:33:27 +04:00
if ( buf_u->base ) {
free(buf_u->base);
2014-02-27 04:39:48 +04:00
}
}
2014-11-05 04:18:47 +03:00
u3_lo_shut(c3y);
2014-02-27 04:39:48 +04:00
}
/* _cttp_ccon_read_clyr_cb()
2014-02-27 04:39:48 +04:00
*/
2014-08-23 01:33:27 +04:00
/*
* `nread` (siz_w) is > 0 if there is data available, 0 if libuv is done reading for
* now, or < 0 on error.
*
* The callee is responsible for closing the stream when an error happens
* by calling uv_close(). Trying to read from the stream again is undefined.
*
* The callee is responsible for freeing the buffer, libuv does not reuse it.
* The buffer may be a null buffer (where buf->base=NULL and buf->len=0) on
* error.
*/
2014-02-27 04:39:48 +04:00
static void
_cttp_ccon_kick_read_clyr_cb(uv_stream_t* tcp_u,
2014-08-23 01:33:27 +04:00
ssize_t siz_w,
2014-08-21 02:09:51 +04:00
const uv_buf_t * buf_u)
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
u3_ccon *coc_u = _cttp_ccon_wax((uv_tcp_t*)tcp_u);
2014-09-11 04:01:32 +04:00
u3_lo_open();
{
2014-08-23 01:33:27 +04:00
if ( siz_w == UV_EOF ) {
2014-11-05 04:18:47 +03:00
_cttp_ccon_fail(coc_u, c3n);
2014-08-23 01:33:27 +04:00
} else if ( siz_w < 0 ) {
uL(fprintf(uH, "cttp: read 1: %s\n", uv_strerror(siz_w)));
2014-11-05 04:18:47 +03:00
_cttp_ccon_fail(coc_u, c3y);
}
else {
2014-08-23 01:33:27 +04:00
_cttp_ccon_pars_shov(coc_u, buf_u->base, siz_w);
}
2014-08-21 02:09:51 +04:00
if ( buf_u->base ) {
free(buf_u->base);
}
}
2014-11-05 04:18:47 +03:00
u3_lo_shut(c3y);
}
/* _cttp_ccon_kick_read_clyr(): start reading on insecure socket.
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_ccon_kick_read_clyr(u3_ccon* coc_u)
{
uv_read_start((uv_stream_t*)&coc_u->wax_u,
_cttp_alloc,
_cttp_ccon_kick_read_clyr_cb);
}
2014-03-15 20:21:50 +04:00
/* _cttp_ccon_kick_read_cryp(): start reading on secure socket.
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_ccon_kick_read_cryp(u3_ccon* coc_u)
2014-03-15 20:21:50 +04:00
{
uv_read_start((uv_stream_t*)&coc_u->wax_u,
_cttp_alloc,
_cttp_ccon_kick_read_cryp_cb);
}
/* _cttp_ccon_kick_handshake(): start ssl handshake.
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_ccon_kick_handshake(u3_ccon* coc_u)
{
2014-09-11 04:01:32 +04:00
coc_u->ssl.ssl_u = SSL_new(u3S);
c3_assert(coc_u->ssl.ssl_u);
coc_u->ssl.rio_u = BIO_new(BIO_s_mem());
c3_assert(coc_u->ssl.rio_u);
coc_u->ssl.wio_u = BIO_new(BIO_s_mem());
c3_assert(coc_u->ssl.wio_u);
BIO_set_nbio(coc_u->ssl.rio_u, 1);
BIO_set_nbio(coc_u->ssl.wio_u, 1);
SSL_set_bio(coc_u->ssl.ssl_u,
coc_u->ssl.rio_u,
coc_u->ssl.wio_u);
SSL_set_connect_state(coc_u->ssl.ssl_u);
SSL_do_handshake(coc_u->ssl.ssl_u);
2014-09-11 04:01:32 +04:00
coc_u->sat_e = u3_csat_sing;
_cttp_ccon_kick(coc_u);
2014-02-27 04:39:48 +04:00
}
/* _cttp_ccon_kick(): start appropriate I/O on client connection.
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_ccon_kick(u3_ccon* coc_u)
2014-02-27 04:39:48 +04:00
{
if ( 0 == coc_u->ceq_u ) {
return;
}
switch ( coc_u->sat_e ) {
default: c3_assert(0);
2014-09-11 04:01:32 +04:00
case u3_csat_dead: {
2014-03-14 21:47:17 +04:00
_cttp_ccon_kick_resolve(coc_u);
2014-02-27 04:39:48 +04:00
break;
}
2014-09-11 04:01:32 +04:00
case u3_csat_addr: {
2014-02-27 04:39:48 +04:00
_cttp_ccon_kick_connect(coc_u);
break;
}
2014-09-11 04:01:32 +04:00
case u3_csat_crop: {
_cttp_ccon_kick_handshake(coc_u);
break;
}
2014-09-11 04:01:32 +04:00
case u3_csat_sing: {
_cttp_ccon_kick_read_cryp(coc_u);
_cttp_ccon_cryp_pull(coc_u);
break;
}
2014-09-11 04:01:32 +04:00
case u3_csat_cryp: {
2014-03-15 20:21:50 +04:00
_cttp_ccon_fill(coc_u);
2014-03-15 20:21:50 +04:00
if ( coc_u->rub_u ) {
_cttp_ccon_kick_write_cryp(coc_u);
}
_cttp_ccon_cryp_pull(coc_u);
break;
}
2014-09-11 04:01:32 +04:00
case u3_csat_clyr: {
2014-02-27 04:39:48 +04:00
_cttp_ccon_fill(coc_u);
if ( coc_u->rub_u ) {
_cttp_ccon_kick_write(coc_u);
}
_cttp_ccon_kick_read_clyr(coc_u);
2014-02-27 04:39:48 +04:00
break;
}
}
}
/* _cttp_ccon_new(): create client connection. Return 0 if url invalid.
*/
2014-09-11 04:01:32 +04:00
static u3_ccon*
2014-11-06 06:10:22 +03:00
_cttp_ccon_new(u3_noun sec, c3_s por_s, c3_c* hot_c)
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
u3_ccon* coc_u = c3_malloc(sizeof(u3_ccon));
2014-02-27 04:39:48 +04:00
2014-09-11 04:01:32 +04:00
memset(coc_u, 0, sizeof(u3_ccon));
2014-02-27 04:39:48 +04:00
coc_u->por_s = por_s;
2014-03-03 02:31:03 +04:00
coc_u->hot_c = hot_c;
2014-02-27 04:39:48 +04:00
coc_u->sec = sec;
coc_u->pre_u = 0;
coc_u->nex_u = 0;
2014-09-11 04:01:32 +04:00
if ( u3_Host.ctp_u.coc_u ) {
coc_u->nex_u = u3_Host.ctp_u.coc_u;
u3_Host.ctp_u.coc_u->pre_u = coc_u;
2014-03-14 21:47:17 +04:00
}
2014-09-11 04:01:32 +04:00
u3_Host.ctp_u.coc_u = coc_u;
2014-02-27 04:39:48 +04:00
return coc_u;
}
2014-08-11 07:09:56 +04:00
#ifndef CTTP_NO_PIPELINE
2014-02-27 04:39:48 +04:00
/* _cttp_ccon_find(): find existing connection for remote server.
*/
2014-09-11 04:01:32 +04:00
static u3_ccon*
2014-11-06 06:10:22 +03:00
_cttp_ccon_find(u3_noun sec, c3_s por_s, c3_c* hot_c)
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
u3_ccon* coc_u;
2014-02-27 04:39:48 +04:00
/* XX: linear search.
*/
2014-09-11 04:01:32 +04:00
for ( coc_u = u3_Host.ctp_u.coc_u; coc_u; coc_u = coc_u->nex_u ) {
2014-03-03 02:31:03 +04:00
if ( !strcmp(hot_c, coc_u->hot_c) && (por_s == coc_u->por_s) ) {
2014-02-27 04:39:48 +04:00
return coc_u;
}
}
return 0;
}
2014-08-11 07:09:56 +04:00
#endif
2014-02-27 04:39:48 +04:00
2014-03-03 02:31:03 +04:00
/* _cttp_ccon(): create or find persistent client connection.
2014-02-27 04:39:48 +04:00
*/
2014-09-11 04:01:32 +04:00
static u3_ccon*
2014-11-06 06:10:22 +03:00
_cttp_ccon(u3_noun sec, c3_s por_s, c3_c* hot_c)
2014-02-27 04:39:48 +04:00
{
2014-08-11 07:09:56 +04:00
#ifndef CTTP_NO_PIPELINE
2014-09-11 04:01:32 +04:00
u3_ccon* coc_c = _cttp_ccon_find(sec, por_s, hot_c);
2014-02-27 04:39:48 +04:00
2014-03-03 02:31:03 +04:00
if ( 0 != coc_c ) {
free(hot_c);
return coc_c;
2014-02-27 04:39:48 +04:00
}
2014-08-11 07:09:56 +04:00
else
#endif
return _cttp_ccon_new(sec, por_s, hot_c);
2014-02-27 04:39:48 +04:00
}
/* _cttp_creq(): cttp request from noun.
*/
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
{
2014-09-11 04:01:32 +04:00
u3_creq* ceq_u = c3_malloc(sizeof(u3_creq));
u3_noun pul = u3h(hes);
u3_noun hat = u3h(pul);
u3_noun sec = u3h(hat);
u3_noun pus = u3h(u3t(hat));
u3_noun hot = u3t(u3t(hat));
u3_noun moh = u3t(hes);
u3_noun meh = u3h(moh);
u3_noun mah = u3h(u3t(moh));
u3_noun moc = u3t(u3t(moh));
2014-02-27 04:39:48 +04:00
memset(ceq_u, 0, sizeof(*ceq_u));
ceq_u->num_l = num_l;
2014-03-03 02:31:03 +04:00
ceq_u->sec = sec;
2014-09-11 04:01:32 +04:00
ceq_u->por_s = (u3_nul == pus) ?
2014-11-05 04:18:47 +03:00
( (c3y == sec) ? 443 : 80 ) : u3t(pus);
2014-09-11 04:01:32 +04:00
ceq_u->hot_c = _cttp_creq_host(u3k(hot)); // XX duplicate work with url
ceq_u->url_c = _cttp_creq_url(u3k(pul));
2014-03-03 02:31:03 +04:00
// uL(fprintf(uH, "requesting %s\n", ceq_u->url_c));
2014-02-27 04:39:48 +04:00
switch ( meh ) {
default: c3_assert(0);
2014-09-11 04:01:32 +04:00
case c3__delt: ceq_u->met_e = u3_hmet_delete; break;
case c3__get: ceq_u->met_e = u3_hmet_get; break;
case c3__head: ceq_u->met_e = u3_hmet_head; break;
case c3__post: ceq_u->met_e = u3_hmet_post; break;
case c3__put: ceq_u->met_e = u3_hmet_put; break;
2014-02-27 04:39:48 +04:00
}
2014-09-11 04:01:32 +04:00
ceq_u->hed_u = _cttp_heds_math(0, u3k(mah));
2014-02-27 04:39:48 +04:00
2014-09-11 04:01:32 +04:00
if ( u3_nul == moc ) {
2014-02-27 04:39:48 +04:00
ceq_u->bod_u = 0;
} else {
2014-09-11 04:01:32 +04:00
ceq_u->bod_u = _cttp_octs_to_bod(u3k(u3t(moc)));
2014-02-27 04:39:48 +04:00
}
ceq_u->nex_u = 0;
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
/* _cttp_ccon_fire_body(): attach body to request buffers.
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_ccon_fire_body(u3_ccon* coc_u, u3_hbod *rub_u)
2014-02-27 04:39:48 +04:00
{
if ( !(coc_u->rub_u) ) {
coc_u->rub_u = coc_u->bur_u = rub_u;
}
else {
coc_u->bur_u->nex_u = rub_u;
coc_u->bur_u = rub_u;
}
}
/* _cttp_ccon_fire_str(): attach string to request buffers.
*/
2014-03-14 21:47:17 +04:00
static void
2014-09-11 04:01:32 +04:00
_cttp_ccon_fire_str(u3_ccon* coc_u, const c3_c* str_c)
2014-02-27 04:39:48 +04:00
{
_cttp_ccon_fire_body(coc_u, _cttp_bod(strlen(str_c), (const c3_y*)str_c));
}
/* _cttp_ccon_fire_heds(): attach output headers.
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_ccon_fire_heds(u3_ccon* coc_u,
u3_hhed* hed_u)
2014-02-27 04:39:48 +04:00
{
while ( hed_u ) {
_cttp_ccon_fire_body(coc_u, _cttp_bud(hed_u->nam_c, hed_u->val_c));
hed_u = hed_u->nex_u;
}
}
/* _cttp_ccon_fire(): load request data for into buffers.
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_ccon_fire(u3_ccon* coc_u, u3_creq* ceq_u)
2014-03-14 21:47:17 +04:00
{
2014-02-27 04:39:48 +04:00
switch ( ceq_u->met_e ) {
default: c3_assert(0);
2014-09-11 04:01:32 +04:00
case u3_hmet_nop: c3_assert(0); break; // XX
case u3_hmet_delete: _cttp_ccon_fire_str(coc_u, "DELETE "); break;
case u3_hmet_get: _cttp_ccon_fire_str(coc_u, "GET "); break;
case u3_hmet_head: _cttp_ccon_fire_str(coc_u, "HEAD "); break;
case u3_hmet_post: _cttp_ccon_fire_str(coc_u, "POST "); break;
case u3_hmet_put: _cttp_ccon_fire_str(coc_u, "PUT "); break;
2014-02-27 04:39:48 +04:00
}
_cttp_ccon_fire_str(coc_u, ceq_u->url_c);
2014-03-03 02:31:03 +04:00
_cttp_ccon_fire_str(coc_u, " HTTP/1.1\r\n");
_cttp_ccon_fire_str(coc_u, "User-Agent: urbit/vere.0.2\r\n");
_cttp_ccon_fire_str(coc_u, "Accept: */*\r\n");
// XX it's more painful than it's worth to deal with SSL+Keepalive
2014-11-05 04:18:47 +03:00
if ( c3n == coc_u->sec ) {
_cttp_ccon_fire_str(coc_u, "Connection: Keep-Alive\r\n");
}
2014-03-03 02:31:03 +04:00
_cttp_ccon_fire_body(coc_u, _cttp_bud("Host", ceq_u->hot_c));
2014-02-27 04:39:48 +04:00
_cttp_ccon_fire_heds(coc_u, ceq_u->hed_u);
if ( !ceq_u->bod_u ) {
2014-03-03 02:31:03 +04:00
_cttp_ccon_fire_str(coc_u, "\r\n");
2014-02-27 04:39:48 +04:00
}
else {
c3_c buf_c[81];
snprintf(buf_c, 80, "content-length: %u\r\n", ceq_u->bod_u->len_w);
_cttp_ccon_fire_str(coc_u, buf_c);
2014-03-03 02:31:03 +04:00
_cttp_ccon_fire_str(coc_u, "\r\n");
_cttp_ccon_fire_body(coc_u, ceq_u->bod_u);
2014-02-27 04:39:48 +04:00
}
}
/* _cttp_ccon_fill(): fill send pipeline as far as possible.
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_ccon_fill(u3_ccon* coc_u)
2014-02-27 04:39:48 +04:00
{
2014-09-11 04:01:32 +04:00
u3_creq* ceq_u = coc_u->ceq_u;
2014-11-06 06:10:22 +03:00
u3_noun fir_t = c3y;
2014-02-27 04:39:48 +04:00
while ( ceq_u ) {
//
// Fun POST handling. To minimize the likelihood that
2014-03-14 21:47:17 +04:00
// a connection accident will disrupt a POST (it can't
2014-02-27 04:39:48 +04:00
// be utterly ruled out, because POST sucks), we ensure
// that there is always some request queued above the
// POST. To do this, we always throw in a NOP before XX should
// the POST. But if there is actually something real
// before the POST, we don't need it.
2014-03-14 21:47:17 +04:00
//
// So before a POST, there is always a sequence of
2014-02-27 04:39:48 +04:00
// idempotent requests, or if nothing else NOT, whose
// completion directly triggers the POST. This way,
// it's very unlikely for idling to break a POST.
//
// Extend for any other non-idempotent method (XX add).
//
2014-11-05 04:18:47 +03:00
if ( (c3n == fir_t) && (u3_hmet_nop == ceq_u->met_e) ) {
2014-02-27 04:39:48 +04:00
ceq_u = ceq_u->nex_u;
continue;
}
2014-11-05 04:18:47 +03:00
if ( (c3n == fir_t) && (u3_hmet_post == ceq_u->met_e) ) {
2014-02-27 04:39:48 +04:00
return;
}
2014-11-05 04:18:47 +03:00
fir_t = c3n;
2014-02-27 04:39:48 +04:00
_cttp_ccon_fire(coc_u, ceq_u);
ceq_u = ceq_u->nex_u;
}
}
/* _cttp_ccon_send(): add I/O operation.
*/
static void
2014-09-11 04:01:32 +04:00
_cttp_ccon_send(u3_ccon* coc_u, u3_creq* ceq_u)
2014-02-27 04:39:48 +04:00
{
2014-11-06 06:10:22 +03:00
u3_noun nou = ((0 == coc_u->ceq_u) ? c3y : c3n);
2014-02-27 04:39:48 +04:00
2014-11-05 04:18:47 +03:00
if ( c3y == nou ) {
2014-02-27 04:39:48 +04:00
c3_assert(0 == coc_u->qec_u);
coc_u->ceq_u = coc_u->qec_u = ceq_u;
_cttp_ccon_kick(coc_u);
}
else {
c3_assert(0 != coc_u->qec_u);
coc_u->qec_u->nex_u = ceq_u;
coc_u->qec_u = ceq_u;
}
2014-03-14 21:47:17 +04:00
}
2014-02-27 04:39:48 +04:00
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
{
2014-09-11 04:01:32 +04:00
if ( u3_nul == cuq ) {
2014-03-03 02:31:03 +04:00
uL(fprintf(uH, "thus: cancel?\n"));
2014-02-27 05:58:40 +04:00
}
else {
2014-09-11 04:01:32 +04:00
u3_creq* ceq_u = _cttp_creq_new(num_l, u3k(u3t(cuq)));
u3_ccon* coc_u = _cttp_ccon(ceq_u->sec, ceq_u->por_s, ceq_u->hot_c);
2014-02-27 05:58:40 +04:00
ceq_u->coc_u = coc_u;
_cttp_ccon_send(coc_u, ceq_u);
}
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
{
c3_i rad;
c3_y buf[4096];
2014-09-11 04:01:32 +04:00
u3_Host.ctp_u.coc_u = 0;
SSL_library_init();
SSL_load_error_strings();
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, NULL);
SSL_CTX_set_default_verify_paths(u3S);
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");
// RAND_status, at least on OS X, never returns true.
// 4096 bytes should be enough entropy for anyone, right?
rad = open("/dev/urandom", O_RDONLY);
if ( 4096 != read(rad, &buf, 4096) ) {
perror("rand-seed");
exit(1);
}
RAND_seed(buf, 4096);
close(rad);
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);
2014-02-27 04:39:48 +04:00
}