1 /* v/cttp.c
2 **
3 */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <fcntl.h>
7 #include <sys/ioctl.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 #include <stdint.h>
11 #include <sys/socket.h>
12 #include <netinet/in.h>
13 #include <uv.h>
14 #include <errno.h>
15 #include <openssl/ssl.h>
16 #include <h2o.h>
17 #include "all.h"
18 #include "vere/vere.h"
19 
20 
21 // XX deduplicate with _http_vec_to_atom
22 /* _cttp_vec_to_atom(): convert h2o_iovec_t to atom (cord)
23 */
24 static u3_noun
_cttp_vec_to_atom(h2o_iovec_t vec_u)25 _cttp_vec_to_atom(h2o_iovec_t vec_u)
26 {
27   return u3i_bytes(vec_u.len, (const c3_y*)vec_u.base);
28 }
29 
30 /* _cttp_bods_free(): free body structure.
31 */
32 static void
_cttp_bods_free(u3_hbod * bod_u)33 _cttp_bods_free(u3_hbod* bod_u)
34 {
35   while ( bod_u ) {
36     u3_hbod* nex_u = bod_u->nex_u;
37 
38     free(bod_u);
39     bod_u = nex_u;
40   }
41 }
42 
43 /* _cttp_bod_new(): create a data buffer
44 */
45 static u3_hbod*
_cttp_bod_new(c3_w len_w,c3_c * hun_c)46 _cttp_bod_new(c3_w len_w, c3_c* hun_c)
47 {
48   u3_hbod* bod_u = c3_malloc(1 + len_w + sizeof(*bod_u));
49   bod_u->hun_y[len_w] = 0;
50   bod_u->len_w = len_w;
51   memcpy(bod_u->hun_y, (const c3_y*)hun_c, len_w);
52 
53   bod_u->nex_u = 0;
54   return bod_u;
55 }
56 
57 /* _cttp_bod_from_hed(): create a data buffer from a header
58 */
59 static u3_hbod*
_cttp_bod_from_hed(u3_hhed * hed_u)60 _cttp_bod_from_hed(u3_hhed* hed_u)
61 {
62   c3_w len_w     = hed_u->nam_w + 2 + hed_u->val_w + 2;
63   u3_hbod* bod_u = c3_malloc(1 + len_w + sizeof(*bod_u));
64   bod_u->hun_y[len_w] = 0;
65 
66   memcpy(bod_u->hun_y, hed_u->nam_c, hed_u->nam_w);
67   memcpy(bod_u->hun_y + hed_u->nam_w, ": ", 2);
68   memcpy(bod_u->hun_y + hed_u->nam_w + 2, hed_u->val_c, hed_u->val_w);
69   memcpy(bod_u->hun_y + hed_u->nam_w + 2 + hed_u->val_w, "\r\n", 2);
70 
71   bod_u->len_w = len_w;
72   bod_u->nex_u = 0;
73 
74   return bod_u;
75 }
76 
77 /* _cttp_bods_to_octs: translate body buffer into octet-stream noun.
78 */
79 static u3_noun
_cttp_bods_to_octs(u3_hbod * bod_u)80 _cttp_bods_to_octs(u3_hbod* bod_u)
81 {
82   c3_w    len_w;
83   c3_y*   buf_y;
84   u3_noun cos;
85 
86   {
87     u3_hbod* bid_u = bod_u;
88 
89     len_w = 0;
90     while ( bid_u ) {
91       len_w += bid_u->len_w;
92       bid_u = bid_u->nex_u;
93     }
94   }
95   buf_y = c3_malloc(1 + len_w);
96   buf_y[len_w] = 0;
97 
98   {
99     c3_y* ptr_y = buf_y;
100 
101     while ( bod_u ) {
102       memcpy(ptr_y, bod_u->hun_y, bod_u->len_w);
103       ptr_y += bod_u->len_w;
104       bod_u = bod_u->nex_u;
105     }
106   }
107   cos = u3i_bytes(len_w, buf_y);
108   free(buf_y);
109   return u3nc(len_w, cos);
110 }
111 
112 /* _cttp_bod_from_octs(): translate octet-stream noun into body.
113 */
114 static u3_hbod*
_cttp_bod_from_octs(u3_noun oct)115 _cttp_bod_from_octs(u3_noun oct)
116 {
117   c3_w len_w;
118 
119   if ( !_(u3a_is_cat(u3h(oct))) ) {     //  2GB max
120     u3m_bail(c3__fail); return 0;
121   }
122   len_w = u3h(oct);
123 
124   {
125     u3_hbod* bod_u = c3_malloc(1 + len_w + sizeof(*bod_u));
126     bod_u->hun_y[len_w] = 0;
127     bod_u->len_w = len_w;
128     u3r_bytes(0, len_w, bod_u->hun_y, u3t(oct));
129 
130     bod_u->nex_u = 0;
131 
132     u3z(oct);
133     return bod_u;
134   }
135 }
136 
137 /* _cttp_bods_to_vec(): translate body buffers to array of h2o_iovec_t
138 */
139 static h2o_iovec_t*
_cttp_bods_to_vec(u3_hbod * bod_u,c3_w * tot_w)140 _cttp_bods_to_vec(u3_hbod* bod_u, c3_w* tot_w)
141 {
142   h2o_iovec_t* vec_u;
143   c3_w len_w;
144 
145   {
146     u3_hbod* bid_u = bod_u;
147     len_w = 0;
148 
149     while( bid_u ) {
150       len_w++;
151       bid_u = bid_u->nex_u;
152     }
153   }
154 
155   vec_u = c3_malloc(sizeof(h2o_iovec_t) * len_w);
156   len_w = 0;
157 
158   while( bod_u ) {
159     vec_u[len_w] = h2o_iovec_init(bod_u->hun_y, bod_u->len_w);
160     len_w++;
161     bod_u = bod_u->nex_u;
162   }
163 
164   *tot_w = len_w;
165 
166   return vec_u;
167 }
168 
169 // XX deduplicate with _http_heds_free
170 /* _cttp_heds_free(): free header linked list
171 */
172 static void
_cttp_heds_free(u3_hhed * hed_u)173 _cttp_heds_free(u3_hhed* hed_u)
174 {
175   while ( hed_u ) {
176     u3_hhed* nex_u = hed_u->nex_u;
177 
178     free(hed_u->nam_c);
179     free(hed_u->val_c);
180     free(hed_u);
181     hed_u = nex_u;
182   }
183 }
184 
185 // XX deduplicate with _http_hed_new
186 /* _cttp_hed_new(): create u3_hhed from nam/val cords
187 */
188 static u3_hhed*
_cttp_hed_new(u3_atom nam,u3_atom val)189 _cttp_hed_new(u3_atom nam, u3_atom val)
190 {
191   c3_w     nam_w = u3r_met(3, nam);
192   c3_w     val_w = u3r_met(3, val);
193   u3_hhed* hed_u = c3_malloc(sizeof(*hed_u));
194 
195   hed_u->nam_c = c3_malloc(1 + nam_w);
196   hed_u->val_c = c3_malloc(1 + val_w);
197   hed_u->nam_c[nam_w] = 0;
198   hed_u->val_c[val_w] = 0;
199   hed_u->nex_u = 0;
200   hed_u->nam_w = nam_w;
201   hed_u->val_w = val_w;
202 
203   u3r_bytes(0, nam_w, (c3_y*)hed_u->nam_c, nam);
204   u3r_bytes(0, val_w, (c3_y*)hed_u->val_c, val);
205 
206   return hed_u;
207 }
208 
209 // XX vv similar to _http_heds_from_noun
210 /* _cttp_heds_math(): create headers from +math
211 */
212 static u3_hhed*
_cttp_heds_math(u3_noun mah)213 _cttp_heds_math(u3_noun mah)
214 {
215   u3_noun hed = u3kdi_tap(mah);
216   u3_noun deh = hed;
217 
218   u3_hhed* hed_u = 0;
219 
220   while ( u3_nul != hed ) {
221     u3_noun nam = u3h(u3h(hed));
222     u3_noun lit = u3t(u3h(hed));
223 
224     while ( u3_nul != lit ) {
225       u3_hhed* nex_u = _cttp_hed_new(nam, u3h(lit));
226       nex_u->nex_u = hed_u;
227 
228       hed_u = nex_u;
229       lit = u3t(lit);
230     }
231 
232     hed = u3t(hed);
233   }
234 
235   u3z(deh);
236   return hed_u;
237 }
238 
239 // XX deduplicate with _http_heds_to_noun
240 /* _cttp_heds_to_noun(): convert h2o_header_t to (list (pair @t @t))
241 */
242 static u3_noun
_cttp_heds_to_noun(h2o_header_t * hed_u,c3_d hed_d)243 _cttp_heds_to_noun(h2o_header_t* hed_u, c3_d hed_d)
244 {
245   u3_noun hed = u3_nul;
246   c3_d dex_d  = hed_d;
247 
248   h2o_header_t deh_u;
249 
250   while ( 0 < dex_d ) {
251     deh_u = hed_u[--dex_d];
252     hed = u3nc(u3nc(_cttp_vec_to_atom(*deh_u.name),
253                     _cttp_vec_to_atom(deh_u.value)), hed);
254   }
255 
256   return hed;
257 }
258 
259 /* _cttp_cres_free(): free a u3_cres.
260 */
261 static void
_cttp_cres_free(u3_cres * res_u)262 _cttp_cres_free(u3_cres* res_u)
263 {
264   _cttp_bods_free(res_u->bod_u);
265   free(res_u);
266 }
267 
268 /* _cttp_cres_new(): create a response
269 */
270 static void
_cttp_cres_new(u3_creq * ceq_u,c3_w sas_w)271 _cttp_cres_new(u3_creq* ceq_u, c3_w sas_w)
272 {
273   ceq_u->res_u = c3_calloc(sizeof(*ceq_u->res_u));
274   ceq_u->res_u->sas_w = sas_w;
275 }
276 
277 /* _cttp_cres_fire_body(): attach response body buffer
278 */
279 static void
_cttp_cres_fire_body(u3_cres * res_u,u3_hbod * bod_u)280 _cttp_cres_fire_body(u3_cres* res_u, u3_hbod* bod_u)
281 {
282   c3_assert(!bod_u->nex_u);
283 
284   if ( !(res_u->bod_u) ) {
285     res_u->bod_u = res_u->dob_u = bod_u;
286   }
287   else {
288     res_u->dob_u->nex_u = bod_u;
289     res_u->dob_u = bod_u;
290   }
291 }
292 
293 /* _cttp_mcut_char(): measure/cut character.
294 */
295 static c3_w
_cttp_mcut_char(c3_c * buf_c,c3_w len_w,c3_c chr_c)296 _cttp_mcut_char(c3_c* buf_c, c3_w len_w, c3_c chr_c)
297 {
298   if ( buf_c ) {
299     buf_c[len_w] = chr_c;
300   }
301   return len_w + 1;
302 }
303 
304 /* _cttp_mcut_cord(): measure/cut cord.
305 */
306 static c3_w
_cttp_mcut_cord(c3_c * buf_c,c3_w len_w,u3_noun san)307 _cttp_mcut_cord(c3_c* buf_c, c3_w len_w, u3_noun san)
308 {
309   c3_w ten_w = u3r_met(3, san);
310 
311   if ( buf_c ) {
312     u3r_bytes(0, ten_w, (c3_y *)(buf_c + len_w), san);
313   }
314   u3z(san);
315   return (len_w + ten_w);
316 }
317 
318 /* _cttp_mcut_path(): measure/cut cord list.
319 */
320 static c3_w
_cttp_mcut_path(c3_c * buf_c,c3_w len_w,c3_c sep_c,u3_noun pax)321 _cttp_mcut_path(c3_c* buf_c, c3_w len_w, c3_c sep_c, u3_noun pax)
322 {
323   u3_noun axp = pax;
324 
325   while ( u3_nul != axp ) {
326     u3_noun h_axp = u3h(axp);
327 
328     len_w = _cttp_mcut_cord(buf_c, len_w, u3k(h_axp));
329     axp = u3t(axp);
330 
331     if ( u3_nul != axp ) {
332       len_w = _cttp_mcut_char(buf_c, len_w, sep_c);
333     }
334   }
335   u3z(pax);
336   return len_w;
337 }
338 
339 /* _cttp_mcut_host(): measure/cut host.
340 */
341 static c3_w
_cttp_mcut_host(c3_c * buf_c,c3_w len_w,u3_noun hot)342 _cttp_mcut_host(c3_c* buf_c, c3_w len_w, u3_noun hot)
343 {
344   len_w = _cttp_mcut_path(buf_c, len_w, '.', u3kb_flop(u3k(hot)));
345   u3z(hot);
346   return len_w;
347 }
348 
349 /* _cttp_mcut_pork(): measure/cut path/extension.
350 */
351 static c3_w
_cttp_mcut_pork(c3_c * buf_c,c3_w len_w,u3_noun pok)352 _cttp_mcut_pork(c3_c* buf_c, c3_w len_w, u3_noun pok)
353 {
354   u3_noun h_pok = u3h(pok);
355   u3_noun t_pok = u3t(pok);
356 
357   len_w = _cttp_mcut_path(buf_c, len_w, '/', u3k(t_pok));
358   if ( u3_nul != h_pok ) {
359     len_w = _cttp_mcut_char(buf_c, len_w, '.');
360     len_w = _cttp_mcut_cord(buf_c, len_w, u3k(u3t(h_pok)));
361   }
362   u3z(pok);
363   return len_w;
364 }
365 
366 /* _cttp_mcut_quay(): measure/cut query.
367 */
368 static c3_w
_cttp_mcut_quay(c3_c * buf_c,c3_w len_w,u3_noun quy)369 _cttp_mcut_quay(c3_c* buf_c, c3_w len_w, u3_noun quy)
370 {
371   if ( u3_nul == quy ) {
372     return len_w;
373   }
374   else {
375     u3_noun i_quy = u3h(quy);
376     u3_noun pi_quy = u3h(i_quy);
377     u3_noun qi_quy = u3t(i_quy);
378     u3_noun t_quy = u3t(quy);
379 
380     len_w = _cttp_mcut_char(buf_c, len_w, '&');
381     len_w = _cttp_mcut_cord(buf_c, len_w, u3k(pi_quy));
382     len_w = _cttp_mcut_char(buf_c, len_w, '=');
383     len_w = _cttp_mcut_cord(buf_c, len_w, u3k(qi_quy));
384 
385     len_w = _cttp_mcut_quay(buf_c, len_w, u3k(t_quy));
386   }
387   u3z(quy);
388   return len_w;
389 }
390 
391 /* _cttp_mcut_url(): measure/cut purl, producing relative URL.
392 */
393 static c3_w
_cttp_mcut_url(c3_c * buf_c,c3_w len_w,u3_noun pul)394 _cttp_mcut_url(c3_c* buf_c, c3_w len_w, u3_noun pul)
395 {
396   u3_noun q_pul = u3h(u3t(pul));
397   u3_noun r_pul = u3t(u3t(pul));
398 
399   len_w = _cttp_mcut_char(buf_c, len_w, '/');
400   len_w = _cttp_mcut_pork(buf_c, len_w, u3k(q_pul));
401 
402   if ( u3_nul != r_pul ) {
403     len_w = _cttp_mcut_char(buf_c, len_w, '?');
404     len_w = _cttp_mcut_quay(buf_c, len_w, u3k(r_pul));
405   }
406   u3z(pul);
407   return len_w;
408 }
409 
410 /* _cttp_creq_port(): stringify port
411 */
412 static c3_c*
_cttp_creq_port(c3_s por_s)413 _cttp_creq_port(c3_s por_s)
414 {
415   c3_c* por_c = c3_malloc(8);
416   snprintf(por_c, 7, "%d", 0xffff & por_s);
417   return por_c;
418 }
419 
420 /* _cttp_creq_url(): construct url from noun.
421 */
422 static c3_c*
_cttp_creq_url(u3_noun pul)423 _cttp_creq_url(u3_noun pul)
424 {
425   c3_w  len_w = _cttp_mcut_url(0, 0, u3k(pul));
426   c3_c* url_c = c3_malloc(1 + len_w);
427 
428   _cttp_mcut_url(url_c, 0, pul);
429   url_c[len_w] = 0;
430 
431   return url_c;
432 }
433 
434 /* _cttp_creq_host(): construct host from noun.
435 */
436 static c3_c*
_cttp_creq_host(u3_noun hot)437 _cttp_creq_host(u3_noun hot)
438 {
439   c3_w  len_w = _cttp_mcut_host(0, 0, u3k(hot));
440   c3_c* hot_c = c3_malloc(1 + len_w);
441 
442   _cttp_mcut_host(hot_c, 0, hot);
443   hot_c[len_w] = 0;
444 
445   return hot_c;
446 }
447 
448 /* _cttp_creq_ip(): stringify ip
449 */
450 static c3_c*
_cttp_creq_ip(c3_w ipf_w)451 _cttp_creq_ip(c3_w ipf_w)
452 {
453   c3_c* ipf_c = c3_malloc(17);
454   snprintf(ipf_c, 16, "%d.%d.%d.%d", (ipf_w >> 24),
455                                      ((ipf_w >> 16) & 255),
456                                      ((ipf_w >> 8) & 255),
457                                      (ipf_w & 255));
458   return ipf_c;
459 }
460 
461 /* _cttp_creq_find(): find a request by number in the client
462 */
463 static u3_creq*
_cttp_creq_find(c3_l num_l)464 _cttp_creq_find(c3_l num_l)
465 {
466   u3_creq* ceq_u = u3_Host.ctp_u.ceq_u;
467 
468   //  XX glories of linear search
469   //
470   while ( ceq_u ) {
471     if ( num_l == ceq_u->num_l ) {
472       return ceq_u;
473     }
474     ceq_u = ceq_u->nex_u;
475   }
476   return 0;
477 }
478 
479 /* _cttp_creq_link(): link request to client
480 */
481 static void
_cttp_creq_link(u3_creq * ceq_u)482 _cttp_creq_link(u3_creq* ceq_u)
483 {
484   ceq_u->nex_u = u3_Host.ctp_u.ceq_u;
485   u3_Host.ctp_u.ceq_u = ceq_u;
486 }
487 
488 /* _cttp_creq_unlink(): unlink request from client
489 */
490 static void
_cttp_creq_unlink(u3_creq * ceq_u)491 _cttp_creq_unlink(u3_creq* ceq_u)
492 {
493   if ( ceq_u->pre_u ) {
494     ceq_u->pre_u->nex_u = ceq_u->nex_u;
495   }
496   else {
497     u3_Host.ctp_u.ceq_u = ceq_u->nex_u;
498   }
499 }
500 
501 /* _cttp_creq_free(): free a u3_creq.
502 */
503 static void
_cttp_creq_free(u3_creq * ceq_u)504 _cttp_creq_free(u3_creq* ceq_u)
505 {
506   _cttp_creq_unlink(ceq_u);
507 
508   _cttp_heds_free(ceq_u->hed_u);
509   // Note: ceq_u->bod_u is covered here
510   _cttp_bods_free(ceq_u->rub_u);
511 
512   if ( ceq_u->res_u ) {
513     _cttp_cres_free(ceq_u->res_u);
514   }
515 
516   free(ceq_u->hot_c);
517   free(ceq_u->por_c);
518   free(ceq_u->url_c);
519   free(ceq_u->vec_u);
520   free(ceq_u);
521 }
522 
523 /* _cttp_creq_new(): create a request from a +hiss noun
524 */
525 static u3_creq*
_cttp_creq_new(c3_l num_l,u3_noun hes)526 _cttp_creq_new(c3_l num_l, u3_noun hes)
527 {
528   u3_creq* ceq_u = c3_calloc(sizeof(*ceq_u));
529 
530   u3_noun pul = u3h(hes);      // +purl
531   u3_noun hat = u3h(pul);      // +hart
532   u3_noun sec = u3h(hat);
533   u3_noun por = u3h(u3t(hat));
534   u3_noun hot = u3t(u3t(hat)); // +host
535   u3_noun moh = u3t(hes);      // +moth
536   u3_noun met = u3h(moh);      // +meth
537   u3_noun mah = u3h(u3t(moh)); // +math
538   u3_noun bod = u3t(u3t(moh));
539 
540   ceq_u->sat_e = u3_csat_init;
541   ceq_u->num_l = num_l;
542   ceq_u->sec   = sec;
543 
544   if ( c3y == u3h(hot) ) {
545     ceq_u->hot_c = _cttp_creq_host(u3k(u3t(hot)));
546   } else {
547     ceq_u->ipf_w = u3r_word(0, u3t(hot));
548     ceq_u->ipf_c = _cttp_creq_ip(ceq_u->ipf_w);
549   }
550 
551   if ( u3_nul != por ) {
552     ceq_u->por_s = u3t(por);
553     ceq_u->por_c = _cttp_creq_port(ceq_u->por_s);
554   }
555 
556   ceq_u->met_m = met;
557   ceq_u->url_c = _cttp_creq_url(u3k(pul));
558   ceq_u->hed_u = _cttp_heds_math(u3k(mah));
559 
560   if ( u3_nul != bod ) {
561     ceq_u->bod_u = _cttp_bod_from_octs(u3k(u3t(bod)));
562   }
563 
564   _cttp_creq_link(ceq_u);
565 
566   u3z(hes);
567   return ceq_u;
568 }
569 
570 /* _cttp_creq_fire_body(): attach body to request buffers.
571 */
572 static void
_cttp_creq_fire_body(u3_creq * ceq_u,u3_hbod * rub_u)573 _cttp_creq_fire_body(u3_creq* ceq_u, u3_hbod *rub_u)
574 {
575   c3_assert(!rub_u->nex_u);
576 
577   if ( !(ceq_u->rub_u) ) {
578     ceq_u->rub_u = ceq_u->bur_u = rub_u;
579   }
580   else {
581     ceq_u->bur_u->nex_u = rub_u;
582     ceq_u->bur_u = rub_u;
583   }
584 }
585 
586 /* _cttp_creq_fire_str(): attach string to request buffers.
587 */
588 static void
_cttp_creq_fire_str(u3_creq * ceq_u,c3_c * str_c)589 _cttp_creq_fire_str(u3_creq* ceq_u, c3_c* str_c)
590 {
591   _cttp_creq_fire_body(ceq_u, _cttp_bod_new(strlen(str_c), str_c));
592 }
593 
594 /* _cttp_creq_fire_heds(): attach output headers.
595 */
596 static void
_cttp_creq_fire_heds(u3_creq * ceq_u,u3_hhed * hed_u)597 _cttp_creq_fire_heds(u3_creq* ceq_u, u3_hhed* hed_u)
598 {
599   while ( hed_u ) {
600     _cttp_creq_fire_body(ceq_u, _cttp_bod_from_hed(hed_u));
601     hed_u = hed_u->nex_u;
602   }
603 }
604 
605 /* _cttp_creq_fire(): load request data for into buffers.
606 */
607 static void
_cttp_creq_fire(u3_creq * ceq_u)608 _cttp_creq_fire(u3_creq* ceq_u)
609 {
610   switch ( ceq_u->met_m ) {
611     default: c3_assert(0);
612     case c3__get:   _cttp_creq_fire_str(ceq_u, "GET ");      break;
613     case c3__put:   _cttp_creq_fire_str(ceq_u, "PUT ");      break;
614     case c3__post:  _cttp_creq_fire_str(ceq_u, "POST ");     break;
615     case c3__head:  _cttp_creq_fire_str(ceq_u, "HEAD ");     break;
616     case c3__conn:  _cttp_creq_fire_str(ceq_u, "CONNECT ");  break;
617     case c3__delt:  _cttp_creq_fire_str(ceq_u, "DELETE ");   break;
618     case c3__opts:  _cttp_creq_fire_str(ceq_u, "OPTIONS ");  break;
619     case c3__trac:  _cttp_creq_fire_str(ceq_u, "TRACE ");    break;
620   }
621   _cttp_creq_fire_str(ceq_u, ceq_u->url_c);
622   _cttp_creq_fire_str(ceq_u, " HTTP/1.1\r\n");
623 
624   {
625     c3_c* hot_c = ceq_u->hot_c ? ceq_u->hot_c : ceq_u->ipf_c;
626     c3_c* hos_c;
627     c3_w  len_w;
628 
629     if ( ceq_u->por_c ) {
630       len_w = 6 + strlen(hot_c) + 1 + strlen(ceq_u->por_c) + 3;
631       hos_c = c3_malloc(len_w);
632       len_w = snprintf(hos_c, len_w, "Host: %s:%s\r\n", hot_c, ceq_u->por_c);
633     }
634     else {
635       len_w = 6 + strlen(hot_c) + 3;
636       hos_c = c3_malloc(len_w);
637       len_w = snprintf(hos_c, len_w, "Host: %s\r\n", hot_c);
638     }
639 
640     _cttp_creq_fire_body(ceq_u, _cttp_bod_new(len_w, hos_c));
641     free(hos_c);
642   }
643 
644   _cttp_creq_fire_heds(ceq_u, ceq_u->hed_u);
645 
646   if ( !ceq_u->bod_u ) {
647     _cttp_creq_fire_body(ceq_u, _cttp_bod_new(2, "\r\n"));
648   }
649   else {
650     c3_c len_c[41];
651     c3_w len_w = snprintf(len_c, 40, "Content-Length: %u\r\n\r\n",
652                                      ceq_u->bod_u->len_w);
653 
654     _cttp_creq_fire_body(ceq_u, _cttp_bod_new(len_w, len_c));
655     _cttp_creq_fire_body(ceq_u, ceq_u->bod_u);
656   }
657 }
658 
659 /* _cttp_creq_quit(): cancel a u3_creq
660 */
661 static void
_cttp_creq_quit(u3_creq * ceq_u)662 _cttp_creq_quit(u3_creq* ceq_u)
663 {
664   if ( u3_csat_addr == ceq_u->sat_e ) {
665     ceq_u->sat_e = u3_csat_quit;
666     return;  // wait to be called again on address resolution
667   }
668 
669   if ( ceq_u->cli_u ) {
670     h2o_http1client_cancel(ceq_u->cli_u);
671   }
672 
673   _cttp_creq_free(ceq_u);
674 }
675 
676 /* _cttp_httr(): dispatch http response to %eyre
677 */
678 static void
_cttp_httr(c3_l num_l,c3_w sas_w,u3_noun mes,u3_noun uct)679 _cttp_httr(c3_l num_l, c3_w sas_w, u3_noun mes, u3_noun uct)
680 {
681   u3_noun htr = u3nt(sas_w, mes, uct);
682   u3_noun pox = u3nt(u3_blip, c3__http, u3_nul);
683 
684   u3v_plan(pox, u3nt(c3__they, num_l, htr));
685 }
686 
687 /* _cttp_creq_quit(): dispatch error response
688 */
689 static void
_cttp_creq_fail(u3_creq * ceq_u,const c3_c * err_c)690 _cttp_creq_fail(u3_creq* ceq_u, const c3_c* err_c)
691 {
692   // XX anything other than a 504?
693   c3_w cod_w = 504;
694 
695   uL(fprintf(uH, "http: fail (%d, %d): %s\r\n", ceq_u->num_l, cod_w, err_c));
696 
697   // XX include err_c as response body?
698   _cttp_httr(ceq_u->num_l, cod_w, u3_nul, u3_nul);
699   _cttp_creq_free(ceq_u);
700 }
701 
702 /* _cttp_creq_quit(): dispatch response
703 */
704 static void
_cttp_creq_respond(u3_creq * ceq_u)705 _cttp_creq_respond(u3_creq* ceq_u)
706 {
707   u3_cres* res_u = ceq_u->res_u;
708 
709   _cttp_httr(ceq_u->num_l, res_u->sas_w, res_u->hed,
710              ( !res_u->bod_u ) ? u3_nul :
711              u3nc(u3_nul, _cttp_bods_to_octs(res_u->bod_u)));
712 
713   _cttp_creq_free(ceq_u);
714 }
715 
716 // XX research: may be called with closed client?
717 /* _cttp_creq_on_body(): cb invoked by h2o upon receiving a response body
718 */
719 static c3_i
_cttp_creq_on_body(h2o_http1client_t * cli_u,const c3_c * err_c)720 _cttp_creq_on_body(h2o_http1client_t* cli_u, const c3_c* err_c)
721 {
722   u3_creq* ceq_u = (u3_creq *)cli_u->data;
723 
724   if ( 0 != err_c && h2o_http1client_error_is_eos != err_c ) {
725     _cttp_creq_fail(ceq_u, err_c);
726     return -1;
727   }
728 
729   h2o_buffer_t* buf_u = cli_u->sock->input;
730 
731   if ( buf_u->size ) {
732     _cttp_cres_fire_body(ceq_u->res_u,
733                          _cttp_bod_new(buf_u->size, buf_u->bytes));
734     h2o_buffer_consume(&cli_u->sock->input, buf_u->size);
735   }
736 
737   if ( h2o_http1client_error_is_eos == err_c ) {
738     _cttp_creq_respond(ceq_u);
739   }
740 
741   return 0;
742 }
743 
744 /* _cttp_creq_on_head(): cb invoked by h2o upon receiving response headers
745 */
746 static h2o_http1client_body_cb
_cttp_creq_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)747 _cttp_creq_on_head(h2o_http1client_t* cli_u, const c3_c* err_c, c3_i ver_i,
748                    c3_i sas_i, h2o_iovec_t sas_u, h2o_header_t* hed_u,
749                    size_t hed_t, c3_i len_i)
750 {
751   u3_creq* ceq_u = (u3_creq *)cli_u->data;
752 
753   if ( 0 != err_c && h2o_http1client_error_is_eos != err_c ) {
754     _cttp_creq_fail(ceq_u, err_c);
755     return 0;
756   }
757 
758   _cttp_cres_new(ceq_u, (c3_w)sas_i);
759   ceq_u->res_u->hed = _cttp_heds_to_noun(hed_u, hed_t);
760 
761   if ( h2o_http1client_error_is_eos == err_c ) {
762     _cttp_creq_respond(ceq_u);
763     return 0;
764   }
765 
766   return _cttp_creq_on_body;
767 }
768 
769 /* _cttp_creq_on_connect(): cb invoked by h2o upon successful connection
770 */
771 static h2o_http1client_head_cb
_cttp_creq_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)772 _cttp_creq_on_connect(h2o_http1client_t* cli_u, const c3_c* err_c,
773                       h2o_iovec_t** vec_p, size_t* vec_t, c3_i* hed_i)
774 {
775   u3_creq* ceq_u = (u3_creq *)cli_u->data;
776 
777   if ( 0 != err_c ) {
778     _cttp_creq_fail(ceq_u, err_c);
779     return 0;
780   }
781 
782   {
783     c3_w len_w;
784     ceq_u->vec_u = _cttp_bods_to_vec(ceq_u->rub_u, &len_w);
785     *vec_t = len_w;
786     *vec_p = ceq_u->vec_u;
787     *hed_i = c3__head == ceq_u->met_m;
788   }
789 
790   return _cttp_creq_on_head;
791 }
792 
793 /* _cttp_creq_connect(): establish connection
794 */
795 static void
_cttp_creq_connect(u3_creq * ceq_u)796 _cttp_creq_connect(u3_creq* ceq_u)
797 {
798   c3_assert(u3_csat_ripe == ceq_u->sat_e);
799   c3_assert(ceq_u->ipf_c);
800 
801   h2o_iovec_t ipf_u = h2o_iovec_init(ceq_u->ipf_c, strlen(ceq_u->ipf_c));
802   c3_s por_s = ceq_u->por_s ? ceq_u->por_s :
803                ( c3y == ceq_u->sec ) ? 443 : 80;
804 
805   // connect by IP
806   h2o_http1client_connect(&ceq_u->cli_u, ceq_u, u3_Host.ctp_u.ctx_u, ipf_u,
807                           por_s, c3y == ceq_u->sec, _cttp_creq_on_connect);
808 
809   // set hostname for TLS handshake
810   if ( ceq_u->hot_c && c3y == ceq_u->sec ) {
811     c3_w len_w  = 1 + strlen(ceq_u->hot_c);
812     c3_c* hot_c = c3_malloc(len_w);
813     strncpy(hot_c, ceq_u->hot_c, len_w);
814 
815     free(ceq_u->cli_u->ssl.server_name);
816     ceq_u->cli_u->ssl.server_name = hot_c;
817   }
818 
819   _cttp_creq_fire(ceq_u);
820 }
821 
822 /* _cttp_creq_resolve_cb(): cb upon IP address resolution
823 */
824 static void
_cttp_creq_resolve_cb(uv_getaddrinfo_t * adr_u,c3_i sas_i,struct addrinfo * aif_u)825 _cttp_creq_resolve_cb(uv_getaddrinfo_t* adr_u,
826                       c3_i              sas_i,
827                       struct addrinfo*  aif_u)
828 {
829   u3_creq* ceq_u = adr_u->data;
830 
831   if ( u3_csat_quit == ceq_u->sat_e ) {
832     return _cttp_creq_quit(ceq_u);;
833   }
834 
835   if ( 0 != sas_i ) {
836     return _cttp_creq_fail(ceq_u, uv_strerror(sas_i));
837   }
838 
839   ceq_u->ipf_w = ntohl(((struct sockaddr_in *)aif_u->ai_addr)->sin_addr.s_addr);
840   ceq_u->ipf_c = _cttp_creq_ip(ceq_u->ipf_w);
841 
842   free(adr_u);
843   uv_freeaddrinfo(aif_u);
844 
845   ceq_u->sat_e = u3_csat_ripe;
846   _cttp_creq_connect(ceq_u);
847 }
848 
849 /* _cttp_creq_resolve(): resolve hostname to IP address
850 */
851 static void
_cttp_creq_resolve(u3_creq * ceq_u)852 _cttp_creq_resolve(u3_creq* ceq_u)
853 {
854   c3_assert(u3_csat_addr == ceq_u->sat_e);
855   c3_assert(ceq_u->hot_c);
856 
857   uv_getaddrinfo_t* adr_u = c3_malloc(sizeof(*adr_u));
858   adr_u->data = ceq_u;
859 
860   struct addrinfo hin_u;
861   memset(&hin_u, 0, sizeof(struct addrinfo));
862 
863   hin_u.ai_family = PF_INET;
864   hin_u.ai_socktype = SOCK_STREAM;
865   hin_u.ai_protocol = IPPROTO_TCP;
866 
867   c3_c* por_c = ceq_u->por_c ? ceq_u->por_c :
868                 ( c3y == ceq_u->sec ) ? "443" : "80";
869 
870   c3_i sas_i;
871 
872   if ( 0 != (sas_i = uv_getaddrinfo(u3L, adr_u, _cttp_creq_resolve_cb,
873                                          ceq_u->hot_c, por_c, &hin_u)) ) {
874     _cttp_creq_fail(ceq_u, uv_strerror(sas_i));
875   }
876 }
877 
878 /* _cttp_creq_start(): start a request
879 */
880 static void
_cttp_creq_start(u3_creq * ceq_u)881 _cttp_creq_start(u3_creq* ceq_u)
882 {
883   if ( ceq_u->ipf_c ) {
884     ceq_u->sat_e = u3_csat_ripe;
885     _cttp_creq_connect(ceq_u);
886   } else {
887     ceq_u->sat_e = u3_csat_addr;
888     _cttp_creq_resolve(ceq_u);
889   }
890 }
891 
892 /* _cttp_init_tls: initialize OpenSSL context
893 */
894 static SSL_CTX*
_cttp_init_tls()895 _cttp_init_tls()
896 {
897   // XX require 1.1.0 and use TLS_client_method()
898   SSL_CTX* tls_u = SSL_CTX_new(SSLv23_client_method());
899   // XX use SSL_CTX_set_max_proto_version() and SSL_CTX_set_min_proto_version()
900   SSL_CTX_set_options(tls_u, SSL_OP_NO_SSLv2 |
901                              SSL_OP_NO_SSLv3 |
902                              // SSL_OP_NO_TLSv1 | // XX test
903                              SSL_OP_NO_COMPRESSION);
904 
905   SSL_CTX_set_verify(tls_u, SSL_VERIFY_PEER, 0);
906   SSL_CTX_set_default_verify_paths(tls_u);
907   SSL_CTX_set_session_cache_mode(tls_u, SSL_SESS_CACHE_OFF);
908   SSL_CTX_set_cipher_list(tls_u,
909                           "ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:"
910                           "ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:"
911                           "RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS");
912 
913   return tls_u;
914 }
915 
916 /* _cttp_init_h2o: initialize h2o client ctx and timeout
917 */
918 static h2o_http1client_ctx_t*
_cttp_init_h2o()919 _cttp_init_h2o()
920 {
921   h2o_timeout_t* tim_u = c3_malloc(sizeof(*tim_u));
922 
923   // XX how long? 1 minute?
924   h2o_timeout_init(u3L, tim_u, 10000);
925 
926   h2o_http1client_ctx_t* ctx_u = c3_calloc(sizeof(*ctx_u));
927   ctx_u->loop = u3L;
928   ctx_u->io_timeout = tim_u;
929 
930   return ctx_u;
931 };
932 
933 /* u3_cttp_ef_thus(): send %thus effect (outgoing request) to cttp.
934 */
935 void
u3_cttp_ef_thus(c3_l num_l,u3_noun cuq)936 u3_cttp_ef_thus(c3_l    num_l,
937                 u3_noun cuq)
938 {
939   u3_creq* ceq_u;
940 
941   if ( u3_nul == cuq ) {
942     ceq_u =_cttp_creq_find(num_l);
943 
944     if ( ceq_u ) {
945       _cttp_creq_quit(ceq_u);
946     }
947   }
948   else {
949     ceq_u = _cttp_creq_new(num_l, u3k(u3t(cuq)));
950     _cttp_creq_start(ceq_u);
951   }
952   u3z(cuq);
953 }
954 
955 /* u3_cttp_io_init(): initialize http client I/O.
956 */
957 void
u3_cttp_io_init()958 u3_cttp_io_init()
959 {
960   u3_Host.ctp_u.tls_u = _cttp_init_tls();
961   u3_Host.ctp_u.ctx_u = _cttp_init_h2o();
962   u3_Host.ctp_u.ctx_u->ssl_ctx = u3_Host.ctp_u.tls_u;
963   u3_Host.ctp_u.ceq_u = 0;
964 }
965 
966 /* u3_cttp_io_poll(): poll kernel for cttp I/O.
967 */
968 void
u3_cttp_io_poll(void)969 u3_cttp_io_poll(void)
970 {
971 }
972 
973 /* u3_cttp_io_exit(): shut down cttp.
974 */
975 void
u3_cttp_io_exit(void)976 u3_cttp_io_exit(void)
977 {
978     SSL_CTX_free(u3_Host.ctp_u.tls_u);
979     free(u3_Host.ctp_u.ctx_u->io_timeout);
980     free(u3_Host.ctp_u.ctx_u);
981 }
982