Coverage Report

Created: 2025-11-16 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebsockets/lib/secure-streams/protocols/ss-h1.c
Line
Count
Source
1
/*
2
 * libwebsockets - small server side websockets and web server implementation
3
 *
4
 * Copyright (C) 2019 - 2020 Andy Green <andy@warmcat.com>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to
8
 * deal in the Software without restriction, including without limitation the
9
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10
 * sell copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22
 * IN THE SOFTWARE.
23
 *
24
 * This is the glue that wires up h1 to Secure Streams.
25
 */
26
27
#include <private-lib-core.h>
28
29
#if !defined(LWS_PLAT_FREERTOS) || defined(LWS_ROLE_H2)
30
#define LWS_WITH_SS_RIDESHARE
31
#endif
32
33
#if defined(LWS_WITH_SS_RIDESHARE)
34
static int
35
ss_http_multipart_parser(lws_ss_handle_t *h, void *in, size_t len)
36
0
{
37
0
  uint8_t *q = (uint8_t *)in;
38
0
  int pending_issue = 0, n = 0;
39
40
41
  /* let's stick it in the boundary state machine first */
42
0
  while (n < (int)len) {
43
0
    if (h->u.http.boundary_seq != h->u.http.boundary_len) {
44
0
      if (q[n] == h->u.http.boundary[h->u.http.boundary_seq])
45
0
        h->u.http.boundary_seq++;
46
0
      else {
47
0
        h->u.http.boundary_seq = 0;
48
0
        h->u.http.boundary_dashes = 0;
49
0
        h->u.http.boundary_post = 0;
50
0
      }
51
0
      goto around;
52
0
    }
53
54
    /*
55
     * We already matched the boundary string, now we're
56
     * looking if there's a -- afterwards
57
     */
58
0
    if (h->u.http.boundary_dashes < 2) {
59
0
      if (q[n] == '-') {
60
0
        h->u.http.boundary_dashes++;
61
0
        goto around;
62
0
      }
63
      /* there was no final -- ... */
64
0
    }
65
66
0
    if (h->u.http.boundary_dashes == 2) {
67
      /*
68
       * It's an EOM boundary: issue pending + multipart EOP
69
       */
70
0
      lwsl_debug("%s: seen EOP, n %d pi %d\n",
71
0
            __func__, n, pending_issue);
72
      /*
73
       * It's possible we already started the decode before
74
       * the end of the last packet.  Then there is no
75
       * remainder to send.
76
       */
77
0
      if (n >= pending_issue + h->u.http.boundary_len +
78
0
          (h->u.http.any ? 2 : 0) + 1) {
79
0
        h->info.rx(ss_to_userobj(h),
80
0
             &q[pending_issue],
81
0
             (unsigned int)(n - pending_issue -
82
0
             h->u.http.boundary_len - 1 -
83
0
             (h->u.http.any ? 2 : 0) /* crlf */),
84
0
           (!h->u.http.som ? LWSSS_FLAG_SOM : 0) |
85
0
           LWSSS_FLAG_EOM | LWSSS_FLAG_RELATED_END);
86
0
        h->u.http.eom = 1;
87
0
      }
88
89
      /*
90
       * Peer may not END_STREAM us
91
       */
92
0
      return 0;
93
      //return -1;
94
0
    }
95
96
    /* how about --boundaryCRLF */
97
98
0
    if (h->u.http.boundary_post < 2) {
99
0
      if ((!h->u.http.boundary_post && q[n] == '\x0d') ||
100
0
          (h->u.http.boundary_post && q[n] == '\x0a')) {
101
0
        h->u.http.boundary_post++;
102
0
        goto around;
103
0
      }
104
      /* there was no final CRLF ... it's wrong */
105
106
0
      return -1;
107
0
    }
108
0
    if (h->u.http.boundary_post != 2)
109
0
      goto around;
110
111
    /*
112
     * We have a starting "--boundaryCRLF" or intermediate
113
     * "CRLF--boundaryCRLF" boundary
114
     */
115
0
    lwsl_debug("%s: b_post = 2 (pi %d)\n", __func__, pending_issue);
116
0
    h->u.http.boundary_seq = 0;
117
0
    h->u.http.boundary_post = 0;
118
119
0
    if (n >= pending_issue && (h->u.http.any || !h->u.http.som)) {
120
      /* Intermediate... do the EOM */
121
0
      lwsl_debug("%s: seen interm EOP n %d pi %d\n", __func__,
122
0
           n, pending_issue);
123
      /*
124
       * It's possible we already started the decode before
125
       * the end of the last packet.  Then there is no
126
       * remainder to send.
127
       */
128
0
      if (n >= pending_issue + h->u.http.boundary_len +
129
0
          (h->u.http.any ? 2 : 0)) {
130
0
        h->info.rx(ss_to_userobj(h), &q[pending_issue],
131
0
             (unsigned int)(n - pending_issue -
132
0
                 h->u.http.boundary_len -
133
0
                 (h->u.http.any ? 2 /* crlf */ : 0)),
134
0
             (!h->u.http.som ? LWSSS_FLAG_SOM : 0) |
135
0
             LWSSS_FLAG_EOM);
136
0
        h->u.http.eom = 1;
137
0
      }
138
0
    }
139
140
    /* Next message starts after this boundary */
141
142
0
    pending_issue = n;
143
0
    if (h->u.http.eom) {
144
      /* reset only if we have sent eom */
145
0
      h->u.http.som = 0;
146
0
      h->u.http.eom = 0;
147
0
    }
148
149
0
around:
150
0
    n++;
151
0
  }
152
153
0
  if (pending_issue != n) {
154
0
    uint8_t oh = 0;
155
156
    /*
157
     * handle the first or last "--boundaryCRLF" case which is not captured in the
158
     * previous loop, on the Bob downchannel (/directive)
159
     *
160
     * probably does not cover the case that one boundary term is separated in multipile
161
     * one callbacks though never see such case
162
     */
163
164
0
    if ((n >= h->u.http.boundary_len) &&
165
0
      h->u.http.boundary_seq == h->u.http.boundary_len &&
166
0
      h->u.http.boundary_post == 2) {
167
168
0
      oh = 1;
169
0
    }
170
171
0
    h->info.rx(ss_to_userobj(h), &q[pending_issue],
172
0
        (unsigned int)(oh ?
173
0
        (n - pending_issue - h->u.http.boundary_len -
174
0
          (h->u.http.any ? 2 : 0)) :
175
0
        (n - pending_issue)),
176
0
         (!h->u.http.som ? LWSSS_FLAG_SOM : 0) |
177
0
           (oh && h->u.http.any ? LWSSS_FLAG_EOM : 0));
178
179
0
    if (oh && h->u.http.any)
180
0
      h->u.http.eom = 1;
181
182
0
    h->u.http.any = 1;
183
0
    h->u.http.som = 1;
184
0
  }
185
186
0
  return 0;
187
0
}
188
#endif
189
190
/*
191
 * Returns 0, or the ss state resp maps on to
192
 */
193
194
static int
195
lws_ss_http_resp_to_state(lws_ss_handle_t *h, int resp)
196
0
{
197
0
  const lws_ss_http_respmap_t *r = h->policy->u.http.respmap;
198
0
  int n = h->policy->u.http.count_respmap;
199
200
0
  while (n--)
201
0
    if (resp == r->resp)
202
0
      return r->state;
203
0
    else
204
0
      r++;
205
206
0
  return 0; /* no hit */
207
0
}
208
209
/*
210
 * This converts any set metadata items into outgoing http headers
211
 */
212
213
static int
214
lws_apply_metadata(lws_ss_handle_t *h, struct lws *wsi, uint8_t *buf,
215
       uint8_t **pp, uint8_t *end)
216
0
{
217
0
  lws_ss_metadata_t *polmd = h->policy->metadata;
218
0
  int m = 0;
219
220
0
  while (polmd) {
221
222
    /* has to have a non-empty header string */
223
224
0
    if (polmd->value__may_own_heap &&
225
0
        ((uint8_t *)polmd->value__may_own_heap)[0] &&
226
0
        h->metadata[m].value__may_own_heap) {
227
0
      if (lws_add_http_header_by_name(wsi,
228
0
          polmd->value__may_own_heap,
229
0
          h->metadata[m].value__may_own_heap,
230
0
          (int)h->metadata[m].length, pp, end))
231
0
      return -1;
232
233
      /*
234
       * Check for the case he's setting a non-zero
235
       * content-length "via the backdoor" metadata-
236
       * driven headers, and set the body_pending()
237
       * state if so...
238
       */
239
240
0
      if (!strncmp(polmd->value__may_own_heap,
241
0
             "content-length", 14) &&
242
0
          atoi(h->metadata[m].value__may_own_heap))
243
0
        lws_client_http_body_pending(wsi, 1);
244
0
    }
245
246
0
    m++;
247
0
    polmd = polmd->next;
248
0
  }
249
250
  /*
251
   * Content-length on POST / PUT / PATCH if we have the length information
252
   */
253
254
0
  if (h->policy->u.http.method && (
255
0
#if defined(LWS_WITH_HTTP_UNCOMMON_HEADERS) || defined(LWS_HTTP_HEADERS_ALL)
256
0
     !strcmp(h->policy->u.http.method, "PATCH") ||
257
0
     !strcmp(h->policy->u.http.method, "PUT") ||
258
0
#endif
259
0
    (!strcmp(h->policy->u.http.method, "POST"))) &&
260
0
      wsi->http.writeable_len) {
261
0
    if (!(h->policy->flags &
262
0
      LWSSSPOLF_HTTP_NO_CONTENT_LENGTH)) {
263
0
      int n = lws_snprintf((char *)buf, 20, "%u",
264
0
        (unsigned int)wsi->http.writeable_len);
265
0
      if (lws_add_http_header_by_token(wsi,
266
0
          WSI_TOKEN_HTTP_CONTENT_LENGTH,
267
0
          buf, n, pp, end))
268
0
        return -1;
269
0
    }
270
0
    lws_client_http_body_pending(wsi, 1);
271
0
  }
272
273
0
  return 0;
274
0
}
275
276
277
#if defined(LWS_WITH_SS_DIRECT_PROTOCOL_STR)
278
static int
279
lws_apply_instant_metadata(lws_ss_handle_t *h, struct lws *wsi, uint8_t *buf,
280
       uint8_t **pp, uint8_t *end)
281
{
282
  lws_ss_metadata_t *imd = h->instant_metadata;
283
284
  while (imd) {
285
    if (imd->name && imd->value__may_own_heap) {
286
      lwsl_debug("%s add header %s %s %d\n", __func__,
287
                     imd->name,
288
                                 (char *)imd->value__may_own_heap,
289
               (int)imd->length);
290
      if (lws_add_http_header_by_name(wsi,
291
          (const unsigned char *)imd->name,
292
          (const unsigned char *)imd->value__may_own_heap,
293
          (int)imd->length, pp, end))
294
      return -1;
295
296
      /* it's possible user set content-length directly */
297
      if (!strncmp(imd->name,
298
             "content-length", 14) &&
299
          atoi(imd->value__may_own_heap))
300
        lws_client_http_body_pending(wsi, 1);
301
302
    }
303
304
    imd = imd->next;
305
  }
306
307
  return 0;
308
}
309
#endif
310
/*
311
 * Check if any metadata headers present in the server headers, and record
312
 * them into the associated metadata item if so.
313
 */
314
315
static int
316
lws_extract_metadata(lws_ss_handle_t *h, struct lws *wsi)
317
0
{
318
0
  lws_ss_metadata_t *polmd = h->policy->metadata, *omd;
319
0
  int n;
320
321
0
  while (polmd) {
322
323
0
    if (polmd->value_is_http_token != LWS_HTTP_NO_KNOWN_HEADER) {
324
325
      /* it's a well-known header token */
326
327
0
      n = lws_hdr_total_length(wsi, polmd->value_is_http_token);
328
0
      if (n) {
329
0
        const char *cp = lws_hdr_simple_ptr(wsi,
330
0
            polmd->value_is_http_token);
331
0
        omd = lws_ss_get_handle_metadata(h, polmd->name);
332
0
        if (!omd || !cp)
333
0
          return 1;
334
335
0
        assert(!strcmp(omd->name, polmd->name));
336
337
        /*
338
         * it's present on the wsi, we want to
339
         * set the related metadata name to it then
340
         */
341
342
0
        _lws_ss_alloc_set_metadata(omd, polmd->name, cp,
343
0
                 (unsigned int)n);
344
345
#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
346
        /*
347
         * ...and because we are doing it from parsing
348
         * onward rx, we want to mark the metadata as
349
         * needing passing to the client
350
         */
351
        omd->pending_onward = 1;
352
#endif
353
0
      }
354
0
    }
355
356
0
#if defined(LWS_WITH_CUSTOM_HEADERS)
357
0
    else
358
359
      /* has to have a non-empty header string */
360
361
0
      if (polmd->value__may_own_heap &&
362
0
          ((uint8_t *)polmd->value__may_own_heap)[0]) {
363
0
        char *p;
364
365
        /*
366
         * Can it be a custom header?
367
         */
368
369
0
        n = lws_hdr_custom_length(wsi, (const char *)
370
0
                polmd->value__may_own_heap,
371
0
                polmd->value_length);
372
0
        if (n > 0) {
373
0
          int r;
374
375
0
          p = lws_malloc((unsigned int)n + 1, __func__);
376
0
          if (!p)
377
0
            return 1;
378
379
          /*
380
           * copy the named custom header value
381
           * into the malloc'd buffer
382
           */
383
384
0
          r = lws_hdr_custom_copy(wsi, p, n + 1,
385
0
                 (const char *)
386
0
                 polmd->value__may_own_heap,
387
0
                 polmd->value_length);
388
389
          /* if needed, free any previous value */
390
391
0
          if (polmd->value_on_lws_heap) {
392
0
            lws_free(
393
0
                polmd->value__may_own_heap);
394
0
            polmd->value_on_lws_heap = 0;
395
0
          }
396
397
0
          if (r < 0) {
398
0
            lws_free(p);
399
400
0
            return 1;
401
0
          }
402
403
0
          omd = lws_ss_get_handle_metadata(h,
404
0
                   polmd->name);
405
0
          if (omd) {
406
407
0
            _lws_ss_set_metadata(omd,
408
0
              polmd->name, p, (size_t)n);
409
0
            omd->value_on_lws_heap = 1;
410
411
#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
412
            omd->pending_onward = 1;
413
#endif
414
0
          }
415
0
        }
416
0
      }
417
0
#endif
418
419
0
    polmd = polmd->next;
420
0
  }
421
422
0
  return 0;
423
0
}
424
425
static const uint8_t blob_idx[] = {
426
  LWS_SYSBLOB_TYPE_AUTH,
427
  LWS_SYSBLOB_TYPE_DEVICE_SERIAL,
428
  LWS_SYSBLOB_TYPE_DEVICE_FW_VERSION,
429
  LWS_SYSBLOB_TYPE_DEVICE_TYPE,
430
};
431
432
int
433
secstream_h1(struct lws *wsi, enum lws_callback_reasons reason, void *user,
434
       void *in, size_t len)
435
0
{
436
0
#if defined(LWS_WITH_SERVER)
437
0
  struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
438
0
#endif
439
0
  lws_ss_handle_t *h = (lws_ss_handle_t *)lws_get_opaque_user_data(wsi);
440
0
  uint8_t buf[LWS_PRE + 1520], *p = &buf[LWS_PRE],
441
0
#if defined(LWS_WITH_SERVER)
442
0
      *start = p,
443
0
#endif
444
0
    *end = &buf[sizeof(buf) - 1];
445
0
  lws_ss_state_return_t r;
446
0
  int f = 0, m, status;
447
0
  char conceal_eom = 0;
448
0
  lws_usec_t inter;
449
0
  size_t buflen;
450
451
0
  switch (reason) {
452
453
0
  case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
454
0
    if (!h) {
455
0
      lwsl_err("%s: CCE with no ss handle %s\n", __func__, lws_wsi_tag(wsi));
456
0
      break;
457
0
    }
458
459
0
    lws_ss_assert_extant(wsi->a.context, wsi->tsi, h);
460
461
0
    assert(h->policy);
462
463
0
#if defined(LWS_WITH_CONMON)
464
0
    lws_conmon_ss_json(h);
465
0
#endif
466
467
0
    lws_metrics_caliper_report_hist(h->cal_txn, wsi);
468
0
    lwsl_info("%s: %s CLIENT_CONNECTION_ERROR: %s\n", __func__,
469
0
        h->lc.gutag, in ? (const char *)in : "none");
470
0
    if (h->ss_dangling_connected) {
471
      /* already disconnected, no action for DISCONNECT_ME */
472
0
      r = lws_ss_event_helper(h, LWSSSCS_DISCONNECTED);
473
0
      if (r != LWSSSSRET_OK)
474
0
        return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
475
0
    } else {
476
      /* already disconnected, no action for DISCONNECT_ME */
477
0
      r = lws_ss_event_helper(h, LWSSSCS_UNREACHABLE);
478
0
      if (r) {
479
0
        if (h->inside_connect) {
480
0
          h->pending_ret = r;
481
0
          break;
482
0
        }
483
484
0
        return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
485
0
      }
486
0
    }
487
488
0
    h->wsi = NULL;
489
0
    r = lws_ss_backoff(h);
490
0
    if (r != LWSSSSRET_OK) {
491
0
      if (h->inside_connect) {
492
0
        h->pending_ret = r;
493
0
        break;
494
0
      }
495
0
      return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
496
0
    }
497
0
    break;
498
499
0
  case LWS_CALLBACK_CLIENT_HTTP_REDIRECT:
500
501
0
    if (!h)
502
0
      return -1;
503
504
0
    if (h->policy->u.http.fail_redirect)
505
0
      lws_system_cpd_set(lws_get_context(wsi),
506
0
             LWS_CPD_CAPTIVE_PORTAL);
507
    /* unless it's explicitly allowed, reject to follow it */
508
0
    return !(h->policy->flags & LWSSSPOLF_ALLOW_REDIRECTS);
509
510
0
  case LWS_CALLBACK_CLOSED_HTTP: /* server */
511
0
  case LWS_CALLBACK_CLOSED_CLIENT_HTTP:
512
0
    if (!h)
513
0
      break;
514
515
0
    h->txn_n_acked = 0;
516
0
    lws_sul_cancel(&h->sul_timeout);
517
518
0
    lws_ss_assert_extant(wsi->a.context, wsi->tsi, h);
519
520
0
#if defined(LWS_WITH_CONMON)
521
0
    if (wsi->conmon.pcol == LWSCONMON_PCOL_NONE) {
522
0
      wsi->conmon.pcol = LWSCONMON_PCOL_HTTP;
523
0
      wsi->conmon.protocol_specific.http.response =
524
0
          (int)lws_http_client_http_response(wsi);
525
0
    }
526
527
0
    lws_conmon_ss_json(h);
528
0
#endif
529
530
0
    lws_metrics_caliper_report_hist(h->cal_txn, wsi);
531
    //lwsl_notice("%s: %s LWS_CALLBACK_CLOSED_CLIENT_HTTP\n",
532
    //    __func__, wsi->lc.gutag);
533
534
0
    h->wsi = NULL;
535
0
    h->hanging_som = 0;
536
0
    h->subseq = 0;
537
538
0
#if defined(LWS_WITH_SERVER)
539
0
    lws_pt_lock(pt, __func__);
540
0
    lws_dll2_remove(&h->cli_list);
541
0
    lws_pt_unlock(pt);
542
0
#endif
543
544
0
    if (h->policy && !(h->policy->flags & LWSSSPOLF_OPPORTUNISTIC) &&
545
0
#if defined(LWS_WITH_SERVER)
546
0
        !(h->info.flags & LWSSSINFLAGS_ACCEPTED) && /* not server */
547
0
#endif
548
0
        !h->txn_ok && !wsi->a.context->being_destroyed) {
549
0
      r = lws_ss_backoff(h);
550
0
      if (r != LWSSSSRET_OK)
551
0
        return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
552
0
      break;
553
0
    } else
554
0
      h->seqstate = SSSEQ_IDLE;
555
556
0
    if (h->ss_dangling_connected) {
557
      /* already disconnected, no action for DISCONNECT_ME */
558
0
      r = lws_ss_event_helper(h, LWSSSCS_DISCONNECTED);
559
0
      if (r == LWSSSSRET_DESTROY_ME)
560
0
        return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
561
0
    }
562
0
    break;
563
564
0
  case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP:
565
566
0
    if (!h)
567
0
      return -1;
568
569
0
    lws_ss_assert_extant(wsi->a.context, wsi->tsi, h);
570
0
    h->wsi = wsi; /* since we accept the wsi is bound to the SS,
571
             * ensure the SS feels the same way about the wsi */
572
573
0
#if defined(LWS_WITH_CONMON)
574
0
    if (wsi->conmon.pcol == LWSCONMON_PCOL_NONE) {
575
0
      wsi->conmon.pcol = LWSCONMON_PCOL_HTTP;
576
0
      wsi->conmon.protocol_specific.http.response =
577
0
          (int)lws_http_client_http_response(wsi);
578
0
    }
579
580
0
    lws_conmon_ss_json(h);
581
0
#endif
582
583
0
    status = (int)lws_http_client_http_response(wsi);
584
0
    lwsl_info("%s: LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP: %d\n", __func__, status);
585
  //  if (!status)
586
      /* it's just telling use we connected / joined the nwsi */
587
  //    break;
588
589
#if defined(LWS_WITH_SYS_METRICS)
590
    if (status) {
591
      lws_snprintf((char *)buf, 10, "%d", status);
592
      lws_metrics_tag_ss_add(h, "http_resp", (char *)buf);
593
    }
594
#endif
595
596
0
    if (status == HTTP_STATUS_SERVICE_UNAVAILABLE /* 503 */ ||
597
0
        status == 429 /* Too many requests */) {
598
      /*
599
       * We understand this attempt failed, and that we should
600
       * conceal this attempt.  If there's a specified
601
       * retry-after, we should use that if larger than our
602
       * computed backoff
603
       */
604
605
0
      inter = 0;
606
0
      lws_http_check_retry_after(wsi, &inter);
607
608
0
      r = _lws_ss_backoff(h, inter);
609
0
      if (r != LWSSSSRET_OK)
610
0
        return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
611
612
0
      return -1; /* end this stream */
613
0
    }
614
615
0
    if (h->policy->u.http.resp_expect)
616
0
      h->u.http.good_respcode =
617
0
          status == h->policy->u.http.resp_expect;
618
0
    else
619
0
      h->u.http.good_respcode = (status >= 200 && status < 300);
620
    // lwsl_err("%s: good resp %d %d\n", __func__, status, h->u.http.good_respcode);
621
622
0
    if (lws_extract_metadata(h, wsi)) {
623
0
      lwsl_info("%s: rx metadata extract failed\n", __func__);
624
625
0
      return -1;
626
0
    }
627
628
0
    if (status) {
629
      /*
630
       * Check and see if it's something from the response
631
       * map, if so, generate the requested status.  If we're
632
       * the proxy onward connection, metadata has priority
633
       * over state updates on the serialization, so the
634
       * state callback will see the right metadata.
635
       */
636
0
      int n = lws_ss_http_resp_to_state(h, status);
637
0
      if (n) {
638
0
        r = lws_ss_event_helper(h, (lws_ss_constate_t)n);
639
0
        if (r != LWSSSSRET_OK)
640
0
          return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi,
641
0
                  &h);
642
0
      }
643
0
    }
644
645
0
    if (h->u.http.good_respcode)
646
0
      lwsl_info("%s: Connected streamtype %s, %d\n", __func__,
647
0
          h->policy->streamtype, status);
648
0
    else
649
0
      if (h->u.http.good_respcode)
650
0
        lwsl_warn("%s: Connected streamtype %s, BAD %d\n",
651
0
            __func__, h->policy->streamtype,
652
0
            status);
653
654
0
    h->hanging_som = 0;
655
656
0
    h->retry = 0;
657
0
    h->seqstate = SSSEQ_CONNECTED;
658
0
    lws_sul_cancel(&h->sul);
659
660
0
    if (h->prev_ss_state != LWSSSCS_CONNECTED) {
661
0
      wsi->client_suppress_CONNECTION_ERROR = 1;
662
      /*
663
       * back-to-back http transactions otherwise go
664
       * DISCONNECTED -> CONNECTED, we should insert
665
       * CONNECTING inbetween
666
       */
667
0
      if (h->prev_ss_state == LWSSSCS_DISCONNECTED) {
668
0
        r = lws_ss_event_helper(h, LWSSSCS_CONNECTING);
669
0
        if (r != LWSSSSRET_OK)
670
0
          return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
671
0
      }
672
0
                       if (h->prev_ss_state != LWSSSCS_CONNECTED && 
673
0
                           h->prev_ss_state != LWSSSCS_QOS_ACK_REMOTE &&
674
0
                           h->prev_ss_state != LWSSSCS_QOS_NACK_REMOTE) {
675
                               // lwsl_ss_notice(h, "HTTP_ESTABLISHED");
676
0
        r = lws_ss_event_helper(h, LWSSSCS_CONNECTED);
677
0
        if (r != LWSSSSRET_OK)
678
0
          return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
679
0
      }
680
0
    }
681
682
    /*
683
     * Since it's an http transaction we initiated... this is
684
     * proof of connection validity
685
     */
686
0
    lws_validity_confirmed(wsi);
687
688
0
#if defined(LWS_WITH_SS_RIDESHARE)
689
690
    /*
691
     * There are two ways we might want to deal with multipart,
692
     * one is pass it through raw (although the user code needs
693
     * a helping hand for learning the boundary), and the other
694
     * is to deframe it and provide basically submessages in the
695
     * different parts.
696
     */
697
698
0
    if (lws_hdr_copy(wsi, (char *)buf, sizeof(buf),
699
0
         WSI_TOKEN_HTTP_CONTENT_TYPE) > 0 &&
700
    /* multipart/form-data;
701
     * boundary=----WebKitFormBoundarycc7YgAPEIHvgE9Bf */
702
703
0
        (!strncmp((char *)buf, "multipart/form-data", 19) ||
704
0
         !strncmp((char *)buf, "multipart/related", 17))) {
705
0
      struct lws_tokenize ts;
706
0
      lws_tokenize_elem e;
707
708
      // puts((const char *)buf);
709
710
0
      memset(&ts, 0, sizeof(ts));
711
0
      ts.start = (char *)buf;
712
0
      ts.len = strlen(ts.start);
713
0
      ts.flags = LWS_TOKENIZE_F_RFC7230_DELIMS |
714
0
          LWS_TOKENIZE_F_SLASH_NONTERM |
715
0
          LWS_TOKENIZE_F_MINUS_NONTERM;
716
717
0
      h->u.http.boundary[0] = '\0';
718
0
      do {
719
0
        e = lws_tokenize(&ts);
720
0
        if (e == LWS_TOKZE_TOKEN_NAME_EQUALS &&
721
0
            !strncmp(ts.token, "boundary", 8) &&
722
0
            ts.token_len == 8) {
723
0
          e = lws_tokenize(&ts);
724
0
          if (e != LWS_TOKZE_TOKEN)
725
0
            goto malformed;
726
0
          h->u.http.boundary[0] = '\x0d';
727
0
          h->u.http.boundary[1] = '\x0a';
728
0
          h->u.http.boundary[2] = '-';
729
0
          h->u.http.boundary[3] = '-';
730
0
          lws_strnncpy(h->u.http.boundary + 4,
731
0
                 ts.token, ts.token_len,
732
0
                 sizeof(h->u.http.boundary) - 4);
733
0
          h->u.http.boundary_len =
734
0
            (uint8_t)(ts.token_len + 4);
735
0
          h->u.http.boundary_seq = 2;
736
0
          h->u.http.boundary_dashes = 0;
737
0
        }
738
0
      } while (e > 0);
739
0
      lwsl_info("%s: multipart boundary '%s' len %d\n", __func__,
740
0
          h->u.http.boundary, h->u.http.boundary_len);
741
742
      /* inform the ss that a related message group begins */
743
744
0
      if ((h->policy->flags & LWSSSPOLF_HTTP_MULTIPART_IN) &&
745
0
          h->u.http.boundary[0])
746
0
        h->info.rx(ss_to_userobj(h), NULL, 0,
747
0
             LWSSS_FLAG_RELATED_START);
748
749
      // lws_header_table_detach(wsi, 0);
750
0
    }
751
0
    break;
752
0
malformed:
753
0
    lwsl_notice("%s: malformed multipart header\n", __func__);
754
0
    return -1;
755
#else
756
    break;
757
#endif
758
759
0
  case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER:
760
0
    if (!h)
761
0
      return -1;
762
0
    if (h->writeable_len)
763
0
      wsi->http.writeable_len = h->writeable_len;
764
765
0
    {
766
0
      uint8_t **p = (uint8_t **)in, *end = (*p) + len,
767
0
        *oin = *(uint8_t **)in;
768
769
    /*
770
     * blob-based headers
771
     */
772
773
0
    for (m = 0; m < _LWSSS_HBI_COUNT; m++) {
774
0
      lws_system_blob_t *ab;
775
0
      int o = 0, n;
776
777
0
      if (!h->policy->u.http.blob_header[m])
778
0
        continue;
779
780
      /*
781
       * To be backward compatible, default is system-wide LWA auth,
782
       * and "http_auth_header" is for default LWA auth, current users do not
783
       * need any change in their policy.
784
       * If user wants different auth/token, need to specify the "use_auth"
785
       * and will be handled after metadata headers are applied.
786
       */
787
788
0
      if (m == LWSSS_HBI_AUTH &&
789
0
          h->policy->u.http.auth_preamble)
790
0
        o = lws_snprintf((char *)buf, sizeof(buf), "%s",
791
0
          h->policy->u.http.auth_preamble);
792
793
0
      if (o > (int)sizeof(buf) - 2)
794
0
        return -1;
795
796
0
      ab = lws_system_get_blob(wsi->a.context, blob_idx[m], 0);
797
0
      if (!ab)
798
0
        return -1;
799
800
0
      buflen = sizeof(buf) - (unsigned int)o - 2u;
801
0
      n = lws_system_blob_get(ab, buf + o, &buflen, 0);
802
0
      if (n < 0)
803
0
        return -1;
804
805
0
      buf[(unsigned int)o + buflen] = '\0';
806
0
      lwsl_debug("%s: adding blob %d: %s\n", __func__, m, buf);
807
808
0
      if (lws_add_http_header_by_name(wsi,
809
0
         (uint8_t *)h->policy->u.http.blob_header[m],
810
0
         buf, (int)((int)buflen + o), p, end))
811
0
        return -1;
812
0
    }
813
814
    /*
815
     * metadata-based headers
816
     */
817
818
0
    if (lws_apply_metadata(h, wsi, buf, p, end))
819
0
      return -1;
820
821
#if defined(LWS_WITH_SS_DIRECT_PROTOCOL_STR)
822
    if (h->policy->flags & LWSSSPOLF_DIRECT_PROTO_STR) {
823
      if (lws_apply_instant_metadata(h, wsi, buf, p, end))
824
        return -1;
825
    }
826
#endif
827
828
#if defined(LWS_WITH_SECURE_STREAMS_AUTH_SIGV4)
829
    if (h->policy->auth && h->policy->auth->type &&
830
        !strcmp(h->policy->auth->type, "sigv4")) {
831
832
      if (lws_ss_apply_sigv4(wsi, h, p, end))
833
        return -1;
834
    }
835
#endif
836
837
838
0
    (void)oin;
839
    //if (*p != oin)
840
    //  lwsl_hexdump_notice(oin, lws_ptr_diff_size_t(*p, oin));
841
842
0
    }
843
844
    /*
845
     * So when proxied, for POST we have to synthesize a CONNECTED
846
     * state, so it can request a writeable and deliver the POST
847
     * body
848
     */
849
0
    if ((h->policy->protocol == LWSSSP_H1 ||
850
0
         h->policy->protocol == LWSSSP_H2) &&
851
0
         h->being_serialized && (
852
0
#if defined(LWS_WITH_HTTP_UNCOMMON_HEADERS) || defined(LWS_HTTP_HEADERS_ALL)
853
0
        !strcmp(h->policy->u.http.method, "PUT") ||
854
0
        !strcmp(h->policy->u.http.method, "PATCH") ||
855
0
#endif
856
0
        !strcmp(h->policy->u.http.method, "POST"))) {
857
858
0
      wsi->client_suppress_CONNECTION_ERROR = 1;
859
0
      if (h->prev_ss_state != LWSSSCS_CONNECTED) {
860
0
        r = lws_ss_event_helper(h, LWSSSCS_CONNECTED);
861
0
        if (r)
862
0
          return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
863
0
      }
864
0
    }
865
866
0
    break;
867
868
  /* chunks of chunked content, with header removed */
869
0
  case LWS_CALLBACK_HTTP_BODY:
870
0
  case LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ:
871
0
    lwsl_debug("%s: RECEIVE_CLIENT_HTTP_READ: read %d\n",
872
0
        __func__, (int)len);
873
0
    if (!h || !h->info.rx)
874
0
      return 0;
875
876
0
#if defined(LWS_WITH_SS_RIDESHARE)
877
0
    if ((h->policy->flags & LWSSSPOLF_HTTP_MULTIPART_IN) &&
878
0
        h->u.http.boundary[0])
879
0
      return ss_http_multipart_parser(h, in, len);
880
0
#endif
881
882
0
    if (!h->subseq) {
883
0
      f |= LWSSS_FLAG_SOM;
884
0
      h->hanging_som = 1;
885
0
      h->subseq = 1;
886
0
    }
887
888
  //  lwsl_notice("%s: HTTP_READ: client side sent len %d fl 0x%x\n",
889
  //        __func__, (int)len, (int)f);
890
891
0
    h->wsi = wsi; /* since we accept the wsi is bound to the SS,
892
             * ensure the SS feels the same way about the wsi */
893
0
    r = h->info.rx(ss_to_userobj(h), (const uint8_t *)in, len, f);
894
0
    if (r != LWSSSSRET_OK)
895
0
      return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
896
897
0
    return 0; /* don't passthru */
898
899
  /* uninterpreted http content */
900
0
  case LWS_CALLBACK_RECEIVE_CLIENT_HTTP:
901
0
    {
902
0
      char *px = (char *)buf + LWS_PRE; /* guarantees LWS_PRE */
903
0
      int lenx = sizeof(buf) - LWS_PRE;
904
905
0
      m = lws_http_client_read(wsi, &px, &lenx);
906
0
      if (m < 0)
907
0
        return m;
908
0
    }
909
0
    lws_set_timeout(wsi, 99, 30);
910
911
0
    return 0; /* don't passthru */
912
913
0
  case LWS_CALLBACK_COMPLETED_CLIENT_HTTP:
914
    // lwsl_debug("%s: LWS_CALLBACK_COMPLETED_CLIENT_HTTP\n", __func__);
915
916
0
    if (!h)
917
0
      return -1;
918
919
0
    if (h->hanging_som) {
920
0
      h->info.rx(ss_to_userobj(h), NULL, 0, LWSSS_FLAG_EOM);
921
0
      h->hanging_som = 0;
922
0
      h->subseq = 0;
923
0
    }
924
925
0
    wsi->http.writeable_len = h->writeable_len = 0;
926
0
    lws_sul_cancel(&h->sul_timeout);
927
928
0
    h->txn_ok = 1;
929
930
#if defined(LWS_WITH_SYS_METRICS)
931
    lws_metrics_tag_ss_add(h, "result",
932
               h->u.http.good_respcode ?
933
               "SS_ACK_REMOTE" : "SS_NACK_REMOTE");
934
#endif
935
936
0
    if (!h->ss_dangling_connected) {
937
0
      r = lws_ss_event_helper(h, LWSSSCS_CONNECTED);
938
0
      if (r != LWSSSSRET_OK)
939
0
        return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
940
0
    }
941
942
0
    if (!h->txn_n_acked) {
943
0
      h->txn_n_acked = 1;
944
0
      r = lws_ss_event_helper(h, h->u.http.good_respcode ?
945
0
            LWSSSCS_QOS_ACK_REMOTE :
946
0
            LWSSSCS_QOS_NACK_REMOTE);
947
0
      if (r != LWSSSSRET_OK)
948
0
        return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
949
0
    }
950
0
    lws_cancel_service(lws_get_context(wsi)); /* abort poll wait */
951
0
    break;
952
953
0
  case LWS_CALLBACK_HTTP_WRITEABLE:
954
0
  case LWS_CALLBACK_CLIENT_HTTP_WRITEABLE:
955
956
0
    if (!h || !h->info.tx) {
957
0
      lwsl_debug("%s: no handle / tx\n", __func__);
958
0
      return 0;
959
0
    }
960
961
0
#if defined(LWS_WITH_SERVER)
962
0
    if (h->txn_resp_pending) {
963
      /*
964
       * If we're going to start sending something, we need to
965
       * to take care of the http response header for it first
966
       */
967
0
      h->txn_resp_pending = 0;
968
969
0
      if (lws_add_http_common_headers(wsi,
970
0
          (unsigned int)(h->txn_resp_set ?
971
0
            (h->txn_resp ? h->txn_resp : 200) :
972
0
            HTTP_STATUS_NOT_FOUND),
973
0
          NULL,
974
0
          h->policy->flags & LWSSSPOLF_HTTP_NO_CONTENT_LENGTH ?
975
0
            LWS_ILLEGAL_HTTP_CONTENT_LEN :
976
0
            h->wsi->http.writeable_len,
977
0
          &p, end))
978
0
        return 1;
979
980
      /*
981
       * metadata-based headers
982
       */
983
984
0
      if (lws_apply_metadata(h, wsi, buf, &p, end))
985
0
        return -1;
986
987
0
      if (lws_finalize_write_http_header(wsi, start, &p, end))
988
0
        return 1;
989
990
      /* write the body separately */
991
0
      lws_callback_on_writable(wsi);
992
993
0
      return 0;
994
0
    }
995
0
#endif
996
997
0
    if (
998
0
#if defined(LWS_WITH_SERVER)
999
0
        !(h->info.flags & LWSSSINFLAGS_ACCEPTED) && /* not accepted */
1000
0
#endif
1001
0
        !h->rideshare)
1002
1003
0
      h->rideshare = h->policy;
1004
1005
0
#if defined(LWS_WITH_SS_RIDESHARE)
1006
0
    if (
1007
0
#if defined(LWS_WITH_SERVER)
1008
0
        !(h->info.flags & LWSSSINFLAGS_ACCEPTED) && /* not accepted */
1009
0
#endif
1010
0
        !h->inside_msg && h->rideshare->u.http.multipart_name)
1011
0
      lws_client_http_multipart(wsi,
1012
0
        h->rideshare->u.http.multipart_name,
1013
0
        h->rideshare->u.http.multipart_filename,
1014
0
        h->rideshare->u.http.multipart_content_type,
1015
0
        (char **)&p, (char *)end);
1016
1017
0
    buflen = lws_ptr_diff_size_t(end, p);
1018
0
    if (h->policy->u.http.multipart_name)
1019
0
      buflen -= 24; /* allow space for end of multipart */
1020
#else
1021
    buflen = lws_ptr_diff_size_t(end, p);
1022
#endif
1023
0
    r = h->info.tx(ss_to_userobj(h), h->txord++, p, &buflen, &f);
1024
0
    if (r == LWSSSSRET_TX_DONT_SEND)
1025
0
      return 0;
1026
0
    if (r < 0)
1027
0
      return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
1028
1029
    // lwsl_notice("%s: WRITEABLE: user tx says len %d fl 0x%x\n",
1030
    //      __func__, (int)buflen, (int)f);
1031
1032
0
    p += buflen;
1033
1034
0
    if (f & LWSSS_FLAG_EOM) {
1035
0
#if defined(LWS_WITH_SERVER)
1036
0
        if (!(h->info.flags & LWSSSINFLAGS_ACCEPTED)) {
1037
0
#endif
1038
0
      conceal_eom = 1;
1039
      /* end of rideshares */
1040
0
      if (!h->rideshare->rideshare_streamtype) {
1041
0
        lws_client_http_body_pending(wsi, 0);
1042
0
#if defined(LWS_WITH_SS_RIDESHARE)
1043
0
        if (h->rideshare->u.http.multipart_name)
1044
0
          lws_client_http_multipart(wsi, NULL, NULL, NULL,
1045
0
            (char **)&p, (char *)end);
1046
0
        conceal_eom = 0;
1047
0
#endif
1048
0
      } else {
1049
0
        h->rideshare = lws_ss_policy_lookup(wsi->a.context,
1050
0
            h->rideshare->rideshare_streamtype);
1051
0
        lws_callback_on_writable(wsi);
1052
0
      }
1053
0
#if defined(LWS_WITH_SERVER)
1054
0
        }
1055
0
#endif
1056
1057
0
      h->inside_msg = 0;
1058
0
    } else {
1059
      /* otherwise we can spin with zero length writes */
1060
0
      if (!f && !lws_ptr_diff(p, buf + LWS_PRE))
1061
0
        break;
1062
0
      h->inside_msg = 1;
1063
0
      lws_callback_on_writable(wsi);
1064
0
    }
1065
1066
0
    lwsl_info("%s: lws_write %d %d\n", __func__,
1067
0
        lws_ptr_diff(p, buf + LWS_PRE), f);
1068
1069
0
    if (lws_write(wsi, buf + LWS_PRE, lws_ptr_diff_size_t(p, buf + LWS_PRE),
1070
0
       (!conceal_eom && (f & LWSSS_FLAG_EOM)) ?
1071
0
            LWS_WRITE_HTTP_FINAL : LWS_WRITE_HTTP) !=
1072
0
        (int)lws_ptr_diff(p, buf + LWS_PRE)) {
1073
0
      lwsl_err("%s: write failed\n", __func__);
1074
0
      return -1;
1075
0
    }
1076
1077
0
#if defined(LWS_WITH_SERVER)
1078
0
    if ((h->info.flags & LWSSSINFLAGS_ACCEPTED) /* server */ &&
1079
0
        (f & LWSSS_FLAG_EOM) &&
1080
0
         lws_http_transaction_completed(wsi))
1081
0
      return -1;
1082
#else
1083
    lws_set_timeout(wsi, 0, 0);
1084
#endif
1085
0
    break;
1086
1087
0
#if defined(LWS_WITH_SERVER)
1088
0
  case LWS_CALLBACK_HTTP:
1089
1090
0
    if (!h)
1091
0
      return -1;
1092
1093
0
    if (h->wsi && h->wsi->mount_hit)
1094
0
      break;
1095
1096
0
    lwsl_info("%s: LWS_CALLBACK_HTTP\n", __func__);
1097
0
    {
1098
1099
0
      h->txn_resp_set = 0;
1100
0
      h->txn_resp_pending = 1;
1101
0
      h->writeable_len = 0;
1102
1103
0
#if defined(LWS_ROLE_H2)
1104
0
      m = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_COLON_METHOD);
1105
0
      if (m) {
1106
0
        if (lws_ss_alloc_set_metadata(h, "method",
1107
0
                lws_hdr_simple_ptr(wsi,
1108
0
                 WSI_TOKEN_HTTP_COLON_METHOD), (unsigned int)m))
1109
0
          return -1;
1110
0
        m = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_COLON_PATH);
1111
0
        if (m && lws_ss_alloc_set_metadata(h, "path",
1112
0
                lws_hdr_simple_ptr(wsi,
1113
0
                 WSI_TOKEN_HTTP_COLON_PATH), (unsigned int)m))
1114
0
          return -1;
1115
0
      } else
1116
0
#endif
1117
0
      {
1118
0
        m = lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI);
1119
0
        if (m) {
1120
0
          if (lws_ss_alloc_set_metadata(h, "path",
1121
0
              lws_hdr_simple_ptr(wsi,
1122
0
                WSI_TOKEN_GET_URI), (unsigned int)m))
1123
0
            return -1;
1124
0
          if (lws_ss_alloc_set_metadata(h, "method", "GET", 3))
1125
0
            return -1;
1126
0
          m = lws_hdr_fragment_length(wsi, WSI_TOKEN_HTTP_AUTHORIZATION, 0);
1127
0
          if (m && lws_ss_alloc_set_metadata(h, "auth",
1128
0
              lws_hdr_simple_ptr(wsi,
1129
0
                WSI_TOKEN_HTTP_AUTHORIZATION), (unsigned int)m))
1130
0
            return -1;
1131
0
        } else {
1132
0
          m = lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI);
1133
0
          if (m) {
1134
0
            if (lws_ss_alloc_set_metadata(h, "path",
1135
0
                lws_hdr_simple_ptr(wsi,
1136
0
                  WSI_TOKEN_POST_URI), (unsigned int)m))
1137
0
              return -1;
1138
0
            if (lws_ss_alloc_set_metadata(h, "method", "POST", 4))
1139
0
              return -1;
1140
0
            m = lws_hdr_fragment_length(wsi, WSI_TOKEN_HTTP_AUTHORIZATION, 0);
1141
0
            if (m && lws_ss_alloc_set_metadata(h, "auth",
1142
0
              lws_hdr_simple_ptr(wsi,
1143
0
                WSI_TOKEN_HTTP_AUTHORIZATION), (unsigned int)m))
1144
0
              return -1;
1145
0
          } else {
1146
0
#if defined(LWS_WITH_HTTP_UNCOMMON_HEADERS) || defined(LWS_HTTP_HEADERS_ALL)
1147
0
            m = lws_hdr_total_length(wsi, WSI_TOKEN_PATCH_URI);
1148
0
            if (m) {
1149
0
              if (lws_ss_alloc_set_metadata(h, "path",
1150
0
                  lws_hdr_simple_ptr(wsi,
1151
0
                    WSI_TOKEN_PATCH_URI), (unsigned int)m))
1152
0
                return -1;
1153
0
              if (lws_ss_alloc_set_metadata(h, "method", "PATCH", 5))
1154
0
                return -1;
1155
0
              m = lws_hdr_fragment_length(wsi, WSI_TOKEN_HTTP_AUTHORIZATION, 0);
1156
0
              if (m && lws_ss_alloc_set_metadata(h, "auth",
1157
0
                lws_hdr_simple_ptr(wsi,
1158
0
                  WSI_TOKEN_HTTP_AUTHORIZATION), (unsigned int)m))
1159
0
                return -1;
1160
0
            }
1161
0
#endif
1162
0
          }
1163
0
        }
1164
0
      }
1165
0
    }
1166
1167
0
    if (!h->ss_dangling_connected) {
1168
#if defined(LWS_WITH_SYS_METRICS)
1169
      /*
1170
       * If any hanging caliper measurement, dump it, and free any tags
1171
       */
1172
      lws_metrics_caliper_report_hist(h->cal_txn, (struct lws *)NULL);
1173
#endif
1174
0
      wsi->client_suppress_CONNECTION_ERROR = 1;
1175
0
      if (h->prev_ss_state != LWSSSCS_CONNECTED) {
1176
0
        r = lws_ss_event_helper(h, LWSSSCS_CONNECTED);
1177
0
        if (r)
1178
0
          return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
1179
0
      }
1180
0
    }
1181
1182
0
    r = lws_ss_event_helper(h, LWSSSCS_SERVER_TXN);
1183
0
    if (r)
1184
0
      return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r,
1185
0
                wsi, &h);
1186
1187
0
    return 0;
1188
0
#endif
1189
1190
0
  default:
1191
0
    break;
1192
0
  }
1193
1194
0
  return lws_callback_http_dummy(wsi, reason, user, in, len);
1195
0
}
1196
1197
const struct lws_protocols protocol_secstream_h1 = {
1198
  "lws-secstream-h1",
1199
  secstream_h1,
1200
  0, 0, 0, NULL, 0
1201
};
1202
1203
/*
1204
 * Munge connect info according to protocol-specific considerations... this
1205
 * usually means interpreting aux in a protocol-specific way and using the
1206
 * pieces at connection setup time, eg, http url pieces.
1207
 *
1208
 * len bytes of buf can be used for things with scope until after the actual
1209
 * connect.
1210
 */
1211
1212
static int
1213
secstream_connect_munge_h1(lws_ss_handle_t *h, char *buf, size_t len,
1214
         struct lws_client_connect_info *i,
1215
         union lws_ss_contemp *ct)
1216
0
{
1217
0
  const char *pbasis = h->policy->u.http.url;
1218
0
  size_t used_in, used_out;
1219
0
  lws_strexp_t exp;
1220
1221
  /* i.path on entry is used to override the policy urlpath if not "" */
1222
1223
0
  if (i->path[0])
1224
0
    pbasis = i->path;
1225
1226
0
  if (!pbasis)
1227
0
    return 0;
1228
1229
  /* uncomment to force h1 */
1230
  // i->alpn = "http/1.1";
1231
1232
0
#if defined(LWS_WITH_SS_RIDESHARE)
1233
0
  if (h->policy->flags & LWSSSPOLF_HTTP_MULTIPART)
1234
0
    i->ssl_connection |= LCCSCF_HTTP_MULTIPART_MIME;
1235
1236
0
  if (h->policy->flags & LWSSSPOLF_HTTP_X_WWW_FORM_URLENCODED)
1237
0
    i->ssl_connection |= LCCSCF_HTTP_X_WWW_FORM_URLENCODED;
1238
0
#endif
1239
1240
0
  if (h->policy->flags & LWSSSPOLF_HTTP_CACHE_COOKIES)
1241
0
    i->ssl_connection |= LCCSCF_CACHE_COOKIES;
1242
1243
  /* protocol aux is the path part */
1244
1245
0
  i->path = buf;
1246
1247
  /* skip the unnessary '/' */
1248
0
  if (*pbasis == '/')
1249
0
    pbasis = pbasis + 1;
1250
1251
0
  buf[0] = '/';
1252
1253
0
  lws_strexp_init(&exp, (void *)h, lws_ss_exp_cb_metadata, buf + 1, len - 1);
1254
1255
0
  if (lws_strexp_expand(&exp, pbasis, strlen(pbasis),
1256
0
            &used_in, &used_out) != LSTRX_DONE)
1257
0
    return 1;
1258
1259
0
  if (used_out + 1 < len - 1)
1260
0
    buf[used_out + 1] = '\0';
1261
1262
0
  __lws_lc_tag_append(&h->lc, buf);
1263
1264
0
  return 0;
1265
0
}
1266
1267
1268
const struct ss_pcols ss_pcol_h1 = {
1269
  "h1",
1270
  "http/1.1",
1271
  &protocol_secstream_h1,
1272
  secstream_connect_munge_h1,
1273
  NULL, NULL
1274
};