@codua/codeoutline-win32-x64 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,835 @@
1
+ -- xhttp_codec.lua - HTTP/1.x parsing helpers shared by demos and workers.
2
+
3
+ local M = {}
4
+ local xutils = require('xutils')
5
+ local xcompress = require('xcompress')
6
+
7
+ -- ============================================================================
8
+ -- Status and Basic Helpers
9
+ -- ============================================================================
10
+
11
+ M.STATUS_TEXT = {
12
+ [101] = 'Switching Protocols',
13
+ [200] = 'OK',
14
+ [201] = 'Created',
15
+ [202] = 'Accepted',
16
+ [204] = 'No Content',
17
+ [301] = 'Moved Permanently',
18
+ [302] = 'Found',
19
+ [304] = 'Not Modified',
20
+ [307] = 'Temporary Redirect',
21
+ [308] = 'Permanent Redirect',
22
+ [400] = 'Bad Request',
23
+ [403] = 'Forbidden',
24
+ [404] = 'Not Found',
25
+ [405] = 'Method Not Allowed',
26
+ [413] = 'Payload Too Large',
27
+ [426] = 'Upgrade Required',
28
+ [500] = 'Internal Server Error',
29
+ [501] = 'Not Implemented',
30
+ }
31
+
32
+ function M.trim(s)
33
+ return tostring(s or ''):gsub('^%s+', ''):gsub('%s+$', '')
34
+ end
35
+
36
+ function M.lower(s)
37
+ return string.lower(tostring(s or ''))
38
+ end
39
+
40
+ function M.clean_header(s)
41
+ return tostring(s or ''):gsub('[\r\n]', ' ')
42
+ end
43
+
44
+ local function add_named_value(t, name, value)
45
+ if not name or name == '' then return end
46
+ local old = t[name]
47
+ if old == nil then
48
+ t[name] = value
49
+ elseif type(old) == 'table' and old._multi == true then
50
+ old[#old + 1] = value
51
+ else
52
+ t[name] = { old, value, _multi = true }
53
+ end
54
+ end
55
+
56
+ function M.uri_decode(s)
57
+ s = tostring(s or ''):gsub('+', ' ')
58
+ return (s:gsub('%%(%x%x)', function(hex)
59
+ return string.char(tonumber(hex, 16))
60
+ end))
61
+ end
62
+
63
+ function M.parse_query(qs)
64
+ local out = {}
65
+ if not qs or qs == '' then return out end
66
+ for part in string.gmatch(qs, '([^&]+)') do
67
+ local eq = part:find('=', 1, true)
68
+ local k, v
69
+ if eq then
70
+ k = M.uri_decode(part:sub(1, eq - 1))
71
+ v = M.uri_decode(part:sub(eq + 1))
72
+ else
73
+ k = M.uri_decode(part)
74
+ v = true
75
+ end
76
+ out[k] = v
77
+ end
78
+ return out
79
+ end
80
+
81
+ function M.parse_content_type(value)
82
+ local s = tostring(value or '')
83
+ local first, rest = s:match('^%s*([^;]*)%s*(.*)$')
84
+ local mime = M.lower(M.trim(first or ''))
85
+ local params = {}
86
+
87
+ rest = rest or ''
88
+ while rest ~= '' do
89
+ rest = rest:gsub('^%s*;%s*', '')
90
+ if rest == '' then break end
91
+
92
+ local key, value_part, next_rest
93
+ key, value_part, next_rest = rest:match('^([^=;%s]+)%s*=%s*"([^"]*)"%s*(.*)$')
94
+ if not key then
95
+ key, value_part, next_rest = rest:match('^([^=;%s]+)%s*=%s*([^;]*)%s*(.*)$')
96
+ end
97
+ if not key then break end
98
+ params[M.lower(M.trim(key))] = M.trim(value_part or '')
99
+ rest = next_rest or ''
100
+ end
101
+
102
+ return mime, params
103
+ end
104
+
105
+ function M.split_lines(s)
106
+ local lines = {}
107
+ local pos = 1
108
+ while true do
109
+ local e = s:find('\r\n', pos, true)
110
+ if not e then
111
+ lines[#lines + 1] = s:sub(pos)
112
+ break
113
+ end
114
+ lines[#lines + 1] = s:sub(pos, e - 1)
115
+ pos = e + 2
116
+ end
117
+ return lines
118
+ end
119
+
120
+ function M.parse_target(target)
121
+ local t = target or '/'
122
+ if t:find('://', 1, true) then
123
+ t = t:match('^https?://[^/]*(/.*)$') or '/'
124
+ end
125
+
126
+ local path = t
127
+ local query_string = ''
128
+ local q = t:find('?', 1, true)
129
+ if q then
130
+ path = t:sub(1, q - 1)
131
+ query_string = t:sub(q + 1)
132
+ end
133
+ if path == '' then path = '/' end
134
+ return path, query_string, M.parse_query(query_string)
135
+ end
136
+
137
+ -- Split an absolute URL into its parts for the HTTP client.
138
+ -- Returns scheme, host, port, path (path includes any query string; defaults
139
+ -- to "/"). Port defaults to 443 for https and 80 otherwise. On a malformed URL
140
+ -- returns nil plus an error message.
141
+ function M.parse_url(url)
142
+ url = tostring(url or '')
143
+ local scheme, rest = url:match('^(%a[%w+.-]*)://(.*)$')
144
+ if not scheme then return nil, 'missing scheme' end
145
+ scheme = M.lower(scheme)
146
+
147
+ local authority, path = rest:match('^([^/]*)(/?.*)$')
148
+ if not path or path == '' then path = '/' end
149
+
150
+ -- Drop any userinfo ("user:pass@").
151
+ local at = authority:find('@', 1, true)
152
+ if at then authority = authority:sub(at + 1) end
153
+
154
+ local host, port
155
+ if authority:sub(1, 1) == '[' then -- bracketed IPv6 literal
156
+ local h, tail = authority:match('^%[([^%]]*)%](.*)$')
157
+ host = h
158
+ port = tail and tail:match('^:(%d+)$') or nil
159
+ else
160
+ host, port = authority:match('^([^:]*):?(%d*)$')
161
+ end
162
+ if not host or host == '' then return nil, 'missing host' end
163
+
164
+ port = tonumber(port)
165
+ if not port then port = (scheme == 'https') and 443 or 80 end
166
+ return scheme, host, port, path
167
+ end
168
+
169
+ function M.parse_chunked(data, pos)
170
+ local chunks = {}
171
+ local len = #data
172
+
173
+ while true do
174
+ local line_end = data:find('\r\n', pos, true)
175
+ if not line_end then return nil, nil, 'incomplete' end
176
+
177
+ local size_line = M.trim(data:sub(pos, line_end - 1))
178
+ local hex = size_line:match('^([0-9a-fA-F]+)')
179
+ local size = hex and tonumber(hex, 16)
180
+ if not size then return nil, nil, 'bad chunk size' end
181
+
182
+ pos = line_end + 2
183
+ if size == 0 then
184
+ if data:sub(pos, pos + 1) == '\r\n' then
185
+ return table.concat(chunks), pos + 2
186
+ end
187
+ local trailer_end = data:find('\r\n\r\n', pos, true)
188
+ if not trailer_end then return nil, nil, 'incomplete' end
189
+ return table.concat(chunks), trailer_end + 4
190
+ end
191
+
192
+ if len < pos + size + 1 then
193
+ return nil, nil, 'incomplete'
194
+ end
195
+
196
+ chunks[#chunks + 1] = data:sub(pos, pos + size - 1)
197
+ pos = pos + size
198
+
199
+ if data:sub(pos, pos + 1) ~= '\r\n' then
200
+ return nil, nil, 'bad chunk terminator'
201
+ end
202
+ pos = pos + 2
203
+ end
204
+ end
205
+
206
+ -- ============================================================================
207
+ -- HTTP Message Parsers
208
+ -- ============================================================================
209
+
210
+ -- Parse only the request line and headers.
211
+ --
212
+ -- The normal parser deliberately waits for a complete body because most HTTP
213
+ -- handlers want a convenient `req.body` string. Smart-HTTP is different: the
214
+ -- body is a git protocol stream and can be much larger than memory. Keeping
215
+ -- this split here lets a caller validate/route a request once its headers have
216
+ -- arrived, then consume the body incrementally without duplicating the header
217
+ -- grammar.
218
+ --
219
+ -- Returns a request-shaped table plus the first body position. The table has
220
+ -- `content_length` for Content-Length framing, `chunked=true` for chunked
221
+ -- framing, and no `body` field. No body-size limit is applied here; callers
222
+ -- that buffer bodies must keep using parse_request, which enforces its limit.
223
+ function M.parse_request_head(data, start_pos, opts)
224
+ opts = opts or {}
225
+ start_pos = start_pos or 1
226
+ local state = opts.state
227
+
228
+ local header_end = data:find('\r\n\r\n', start_pos, true)
229
+ if not header_end then
230
+ local max_header_size = opts.max_header_size or (64 * 1024)
231
+ if #data - start_pos + 1 > max_header_size then
232
+ return nil, nil, 'request too large'
233
+ end
234
+ return nil, nil, 'incomplete'
235
+ end
236
+
237
+ local header_block = data:sub(start_pos, header_end - 1)
238
+ local lines = M.split_lines(header_block)
239
+ local request_line = lines[1] or ''
240
+ local method, target, version = request_line:match('^(%S+)%s+(%S+)%s+(HTTP/%d%.%d)$')
241
+ if not method then
242
+ return nil, nil, 'bad request line'
243
+ end
244
+
245
+ local headers = {}
246
+ local header_list = {}
247
+ for i = 2, #lines do
248
+ local line = lines[i]
249
+ if line ~= '' then
250
+ local colon = line:find(':', 1, true)
251
+ if not colon then
252
+ return nil, nil, 'bad header'
253
+ end
254
+ local name = M.lower(M.trim(line:sub(1, colon - 1)))
255
+ local value = M.trim(line:sub(colon + 1))
256
+ header_list[#header_list + 1] = { name = name, value = value }
257
+ if headers[name] then
258
+ headers[name] = headers[name] .. ', ' .. value
259
+ else
260
+ headers[name] = value
261
+ end
262
+ end
263
+ end
264
+
265
+ local body_start = header_end + 4
266
+ local transfer_encoding = M.lower(headers['transfer-encoding'])
267
+ local chunked = transfer_encoding:find('chunked', 1, true) ~= nil
268
+ local content_length
269
+ if chunked then
270
+ content_length = nil
271
+ else
272
+ content_length = tonumber(headers['content-length'] or '0')
273
+ if content_length == nil then content_length = -1 end
274
+ if content_length < 0 then
275
+ return nil, nil, 'bad content length'
276
+ end
277
+ end
278
+
279
+ local path, query_string, query = M.parse_target(target)
280
+ local connection = M.lower(headers['connection'])
281
+ local keep_alive = version == 'HTTP/1.1'
282
+ if connection:find('close', 1, true) then
283
+ keep_alive = false
284
+ elseif connection:find('keep%-alive') or connection:find('keep-alive', 1, true) then
285
+ keep_alive = true
286
+ end
287
+
288
+ return {
289
+ method = method,
290
+ target = target,
291
+ path = path,
292
+ query_string = query_string,
293
+ query = query,
294
+ version = version,
295
+ headers = headers,
296
+ header_list = header_list,
297
+ body_start = body_start,
298
+ content_length = content_length,
299
+ chunked = chunked,
300
+ keep_alive = keep_alive,
301
+ peer_ip = state and state.ip or nil,
302
+ peer_port = state and state.port or nil,
303
+ }, body_start
304
+ end
305
+
306
+ function M.parse_request(data, start_pos, opts)
307
+ opts = opts or {}
308
+ start_pos = start_pos or 1
309
+ local max_request_size = opts.max_request_size or (16 * 1024 * 1024)
310
+ local body = ''
311
+ local head, body_start, err = M.parse_request_head(data, start_pos, {
312
+ state = opts.state,
313
+ max_header_size = opts.max_header_size,
314
+ })
315
+ if not head then return nil, body_start, err end
316
+ local next_pos = body_start
317
+
318
+ if head.chunked then
319
+ local chunk_body, chunk_next, chunk_err = M.parse_chunked(data, body_start)
320
+ if chunk_err == 'incomplete' then return nil, nil, 'incomplete' end
321
+ if chunk_err then return nil, nil, chunk_err end
322
+ body = chunk_body
323
+ next_pos = chunk_next
324
+ else
325
+ local content_length = head.content_length or 0
326
+ if content_length > max_request_size then
327
+ return nil, nil, 'request body too large'
328
+ end
329
+ if #data < body_start + content_length - 1 then
330
+ return nil, nil, 'incomplete'
331
+ end
332
+ if content_length > 0 then
333
+ body = data:sub(body_start, body_start + content_length - 1)
334
+ end
335
+ next_pos = body_start + content_length
336
+ end
337
+
338
+ if next_pos - start_pos > max_request_size then
339
+ return nil, nil, 'request too large'
340
+ end
341
+
342
+ -- Optional: decompress the body in-place when Content-Encoding is one of
343
+ -- the formats we recognise. Capped by max_decompressed_size to guard
344
+ -- against zip-bomb-style payloads; default 16x the wire size when not set.
345
+ local content_encoding = nil
346
+ local raw_encoding = head.headers['content-encoding']
347
+ if opts.decompress and body ~= '' and raw_encoding then
348
+ local enc = M.lower(raw_encoding)
349
+ if enc == 'gzip' or enc == 'x-gzip' or enc == 'deflate' then
350
+ local max_dec = opts.max_decompressed_size
351
+ or (max_request_size > 0 and max_request_size)
352
+ or (16 * 1024 * 1024)
353
+ local plain, err = M.decompress_body(body, enc, max_dec)
354
+ if not plain then
355
+ return nil, nil, 'decompress: ' .. tostring(err)
356
+ end
357
+ content_encoding = enc
358
+ body = plain
359
+ -- Keep the original header value in the table so handlers can see
360
+ -- it, but null out content-length since the body length changed.
361
+ head.headers['content-length'] = tostring(#body)
362
+ end
363
+ end
364
+
365
+ head.body = body
366
+ head.content_encoding = content_encoding -- nil unless we decompressed
367
+ return head, next_pos
368
+ end
369
+
370
+ function M.parse_response(data, pos, opts)
371
+ opts = opts or {}
372
+ pos = pos or 1
373
+
374
+ local header_end = data:find('\r\n\r\n', pos, true)
375
+ if not header_end then return nil, nil, 'incomplete' end
376
+
377
+ local lines = M.split_lines(data:sub(pos, header_end - 1))
378
+ local version, status = (lines[1] or ''):match('^(HTTP/%d%.%d)%s+(%d%d%d)')
379
+ status = tonumber(status)
380
+ if not version or not status then
381
+ return nil, nil, 'bad response line'
382
+ end
383
+
384
+ local headers = {}
385
+ local header_list = {}
386
+ for i = 2, #lines do
387
+ local line = lines[i]
388
+ local colon = line:find(':', 1, true)
389
+ if colon then
390
+ local name = M.lower(M.trim(line:sub(1, colon - 1)))
391
+ local value = M.trim(line:sub(colon + 1))
392
+ header_list[#header_list + 1] = { name = name, value = value }
393
+ if headers[name] then
394
+ headers[name] = headers[name] .. ', ' .. value
395
+ else
396
+ headers[name] = value
397
+ end
398
+ end
399
+ end
400
+
401
+ local body_start = header_end + 4
402
+
403
+ -- Responses to HEAD, plus 1xx/204/304, carry no body regardless of headers.
404
+ local no_body = opts.method == 'HEAD'
405
+ or status == 204 or status == 304
406
+ or (status >= 100 and status < 200)
407
+
408
+ local body, next_pos
409
+ local transfer_encoding = M.lower(headers['transfer-encoding'] or '')
410
+
411
+ if no_body then
412
+ body, next_pos = '', body_start
413
+ elseif transfer_encoding:find('chunked', 1, true) then
414
+ local chunk_body, chunk_next, chunk_err = M.parse_chunked(data, body_start)
415
+ if chunk_err == 'incomplete' then return nil, nil, 'incomplete' end
416
+ if chunk_err then return nil, nil, chunk_err end
417
+ body, next_pos = chunk_body, chunk_next
418
+ elseif headers['content-length'] then
419
+ local body_len = tonumber(headers['content-length']) or -1
420
+ if body_len < 0 then return nil, nil, 'bad content length' end
421
+ if #data < body_start + body_len - 1 then
422
+ return nil, nil, 'incomplete'
423
+ end
424
+ body = body_len > 0 and data:sub(body_start, body_start + body_len - 1) or ''
425
+ next_pos = body_start + body_len
426
+ elseif opts.eof then
427
+ -- No Content-Length and no chunking: the body runs to end of stream
428
+ -- (HTTP/1.0 or "Connection: close" framing). The caller passes eof=true
429
+ -- once the peer has closed, so the remaining bytes are the full body.
430
+ body = data:sub(body_start)
431
+ next_pos = #data + 1
432
+ else
433
+ return nil, nil, 'incomplete'
434
+ end
435
+
436
+ -- Optional: transparently decode a compressed body (mirrors parse_request).
437
+ local content_encoding = nil
438
+ local raw_encoding = headers['content-encoding']
439
+ if opts.decompress and body ~= '' and raw_encoding then
440
+ local enc = M.lower(raw_encoding)
441
+ if enc == 'gzip' or enc == 'x-gzip' or enc == 'deflate' then
442
+ local max_dec = opts.max_decompressed_size or (16 * 1024 * 1024)
443
+ local plain, derr = M.decompress_body(body, enc, max_dec)
444
+ if not plain then
445
+ return nil, nil, 'decompress: ' .. tostring(derr)
446
+ end
447
+ content_encoding = enc
448
+ body = plain
449
+ headers['content-length'] = tostring(#body)
450
+ end
451
+ end
452
+
453
+ return {
454
+ version = version,
455
+ status = status,
456
+ headers = headers,
457
+ header_list = header_list,
458
+ body = body,
459
+ content_encoding = content_encoding, -- nil unless we decompressed
460
+ }, next_pos
461
+ end
462
+
463
+ -- ============================================================================
464
+ -- Body Decoders
465
+ -- ============================================================================
466
+
467
+ function M.form(req)
468
+ local body = type(req) == 'table' and req.body or req
469
+ return M.parse_query(tostring(body or ''))
470
+ end
471
+
472
+ M.JSON_NULL = xutils.json_null
473
+ M.json_pack = xutils.json_pack
474
+ M.json_unpack = xutils.json_unpack
475
+
476
+ function M.json(req)
477
+ local body = type(req) == 'table' and req.body or req
478
+ return xutils.json_unpack(tostring(body or ''))
479
+ end
480
+
481
+ local function parse_header_block(block)
482
+ local headers = {}
483
+ local header_list = {}
484
+ for _, line in ipairs(M.split_lines(block or '')) do
485
+ local colon = line:find(':', 1, true)
486
+ if colon then
487
+ local name = M.lower(M.trim(line:sub(1, colon - 1)))
488
+ local value = M.trim(line:sub(colon + 1))
489
+ header_list[#header_list + 1] = { name = name, value = value }
490
+ if headers[name] then
491
+ headers[name] = headers[name] .. ', ' .. value
492
+ else
493
+ headers[name] = value
494
+ end
495
+ end
496
+ end
497
+ return headers, header_list
498
+ end
499
+
500
+ local function parse_disposition(value)
501
+ local kind, params = M.parse_content_type(value)
502
+ return kind, params
503
+ end
504
+
505
+ function M.multipart(req)
506
+ local body = type(req) == 'table' and req.body or ''
507
+ local headers = type(req) == 'table' and req.headers or {}
508
+ local mime, params = M.parse_content_type(headers['content-type'] or '')
509
+ if mime ~= 'multipart/form-data' then
510
+ return nil, 'content-type is not multipart/form-data'
511
+ end
512
+ local boundary = params.boundary
513
+ if not boundary or boundary == '' then
514
+ return nil, 'missing multipart boundary'
515
+ end
516
+
517
+ local marker = '--' .. boundary
518
+ local pos = body:find(marker, 1, true)
519
+ if not pos then return nil, 'multipart boundary not found' end
520
+ pos = pos + #marker
521
+
522
+ local out = { fields = {}, files = {}, parts = {} }
523
+ while pos <= #body do
524
+ local trailer = body:sub(pos, pos + 1)
525
+ if trailer == '--' then
526
+ return out
527
+ end
528
+ if trailer ~= '\r\n' then
529
+ return nil, 'bad multipart boundary'
530
+ end
531
+ pos = pos + 2
532
+
533
+ local header_end = body:find('\r\n\r\n', pos, true)
534
+ if not header_end then return nil, 'incomplete multipart header' end
535
+
536
+ local part_headers, part_header_list = parse_header_block(body:sub(pos, header_end - 1))
537
+ local data_start = header_end + 4
538
+ local next_boundary = body:find('\r\n' .. marker, data_start, true)
539
+ if not next_boundary then return nil, 'multipart closing boundary not found' end
540
+
541
+ local data = body:sub(data_start, next_boundary - 1)
542
+ local _, disp_params = parse_disposition(part_headers['content-disposition'] or '')
543
+ local name = disp_params.name
544
+ local filename = disp_params.filename
545
+ local part = {
546
+ headers = part_headers,
547
+ header_list = part_header_list,
548
+ name = name,
549
+ filename = filename,
550
+ content_type = part_headers['content-type'],
551
+ data = data,
552
+ size = #data,
553
+ }
554
+
555
+ out.parts[#out.parts + 1] = part
556
+ if filename and filename ~= '' then
557
+ add_named_value(out.files, name, part)
558
+ else
559
+ add_named_value(out.fields, name, data)
560
+ end
561
+
562
+ pos = next_boundary + 2 + #marker
563
+ end
564
+
565
+ return nil, 'unterminated multipart body'
566
+ end
567
+
568
+ -- ============================================================================
569
+ -- Response Normalization and Sending
570
+ -- ============================================================================
571
+
572
+ local function read_file(path)
573
+ local f = io.open(path, 'rb')
574
+ if not f then return nil end
575
+ local data = f:read('*a')
576
+ f:close()
577
+ return data
578
+ end
579
+
580
+ -- ============================================================================
581
+ -- Content-Encoding (gzip / deflate)
582
+ -- ============================================================================
583
+ --
584
+ -- HTTP semantics:
585
+ -- Content-Encoding: gzip -> gzip stream (RFC 1952)
586
+ -- Content-Encoding: deflate -> *zlib* stream (RFC 1950) per RFC 7230;
587
+ -- NOT raw deflate. This is the historical
588
+ -- browser/server gotcha. We treat HTTP
589
+ -- "deflate" as xcompress.zlib_*.
590
+
591
+ -- Content-Type prefixes we won't re-compress (already compressed bytes).
592
+ local DO_NOT_COMPRESS = {
593
+ 'image/',
594
+ 'video/',
595
+ 'audio/',
596
+ 'application/zip',
597
+ 'application/gzip',
598
+ 'application/x-gzip',
599
+ 'application/x-7z-compressed',
600
+ 'application/x-rar-compressed',
601
+ 'application/octet-stream',
602
+ }
603
+
604
+ local function compressible_content_type(ct)
605
+ if not ct or ct == '' then return true end
606
+ local lc = M.lower(ct)
607
+ for i = 1, #DO_NOT_COMPRESS do
608
+ local p = DO_NOT_COMPRESS[i]
609
+ if lc:sub(1, #p) == p then return false end
610
+ end
611
+ return true
612
+ end
613
+
614
+ -- Pick the best response encoding from a client's Accept-Encoding header.
615
+ -- Returns 'gzip' | 'deflate' | nil. Server preference: gzip > deflate.
616
+ -- (q-values and 'identity;q=0' refusal aren't honoured -- if you need that,
617
+ -- parse the header more carefully and pass an explicit choice in.)
618
+ function M.select_encoding(accept_encoding)
619
+ if not accept_encoding or accept_encoding == '' then return nil end
620
+ local ae = M.lower(accept_encoding)
621
+ if ae:find('gzip', 1, true) then return 'gzip' end
622
+ if ae:find('deflate', 1, true) then return 'deflate' end
623
+ return nil
624
+ end
625
+
626
+ function M.compress_body(body, encoding, level)
627
+ if encoding == 'gzip' then return xcompress.gzip(body, level) end
628
+ if encoding == 'deflate' then return xcompress.zlib_compress(body, level) end
629
+ return nil, 'unknown encoding: ' .. tostring(encoding)
630
+ end
631
+
632
+ function M.decompress_body(body, encoding, max_size)
633
+ encoding = M.lower(encoding or '')
634
+ if encoding == 'gzip' or encoding == 'x-gzip' then
635
+ return xcompress.gunzip(body, max_size)
636
+ end
637
+ if encoding == 'deflate' then
638
+ return xcompress.zlib_decompress(body, max_size)
639
+ end
640
+ return nil, 'unsupported encoding: ' .. encoding
641
+ end
642
+
643
+ -- ============================================================================
644
+ -- HTTP -> HTTPS upgrade helpers (force-HTTPS redirect + HSTS)
645
+ -- ============================================================================
646
+ --
647
+ -- "Upgrading" a plaintext HTTP service to HTTPS in practice means two things:
648
+ -- 1. redirect plaintext requests to the https:// URL, and
649
+ -- 2. tell compliant clients to stick to HTTPS via HSTS
650
+ -- (Strict-Transport-Security).
651
+ -- (RFC 2817 "Upgrade: TLS" exists but no browser implements it, so the
652
+ -- redirect + HSTS pair is the real-world mechanism.) The xhttp worker can apply
653
+ -- both from config; these helpers are also usable directly from app handlers.
654
+
655
+ -- Build a Strict-Transport-Security header value from a flexible spec:
656
+ -- true -> 'max-age=31536000' (1 year)
657
+ -- <number> -> 'max-age=<number>'
658
+ -- '<string>' -> used verbatim
659
+ -- { max_age=, include_subdomains=, preload= }
660
+ function M.hsts_value(spec)
661
+ if spec == nil or spec == false then return nil end
662
+ if spec == true then return 'max-age=31536000' end
663
+ if type(spec) == 'number' then return 'max-age=' .. tostring(math.floor(spec)) end
664
+ if type(spec) == 'string' then return spec end
665
+ if type(spec) == 'table' then
666
+ local age = tonumber(spec.max_age) or 31536000
667
+ local v = 'max-age=' .. tostring(math.floor(age))
668
+ if spec.include_subdomains then v = v .. '; includeSubDomains' end
669
+ if spec.preload then v = v .. '; preload' end
670
+ return v
671
+ end
672
+ return 'max-age=31536000'
673
+ end
674
+
675
+ -- Compute the https:// URL a plaintext request should be redirected to. Host is
676
+ -- taken from req.headers.host (port stripped) unless opts.host overrides it;
677
+ -- the port is appended only when it is not the default 443.
678
+ function M.https_url(req, opts)
679
+ opts = opts or {}
680
+ local host = opts.host
681
+ or (req.headers and req.headers['host'])
682
+ or 'localhost'
683
+ host = tostring(host):gsub(':%d+$', '') -- strip any :port (keeps [ipv6])
684
+ local port = tonumber(opts.https_port or opts.port)
685
+ local hostport = host
686
+ if port and port ~= 443 then
687
+ hostport = host .. ':' .. tostring(port)
688
+ end
689
+ local target = req.target or req.path or '/'
690
+ if target == '' then target = '/' end
691
+ return 'https://' .. hostport .. target
692
+ end
693
+
694
+ -- Build a redirect response table to the https:// version of req.
695
+ -- opts.status -- 301 (default), 302, 307 or 308
696
+ -- opts.https_port -- target HTTPS port (default 443)
697
+ -- opts.host -- override Host
698
+ -- opts.hsts -- HSTS spec (see hsts_value); included on the redirect too
699
+ function M.https_redirect(req, opts)
700
+ opts = opts or {}
701
+ local headers = { ['Location'] = M.https_url(req, opts) }
702
+ local hsts = M.hsts_value(opts.hsts)
703
+ if hsts then headers['Strict-Transport-Security'] = hsts end
704
+ return {
705
+ status = tonumber(opts.status) or 301,
706
+ headers = headers,
707
+ body = opts.body or '',
708
+ }
709
+ end
710
+
711
+ function M.normalize_response(req, resp, opts)
712
+ opts = opts or {}
713
+ if resp == nil then
714
+ resp = { status = 404, body = 'not found\n' }
715
+ elseif type(resp) == 'string' or type(resp) == 'number' or type(resp) == 'boolean' then
716
+ resp = { status = 200, body = tostring(resp) }
717
+ elseif type(resp) ~= 'table' then
718
+ resp = { status = 500, body = 'invalid response\n' }
719
+ end
720
+
721
+ local status = tonumber(resp.status or resp.code or 200) or 200
722
+ local file_path = resp.file or resp.file_path
723
+ local body = resp.body
724
+ if body == nil then body = '' end
725
+ if type(body) ~= 'string' then body = tostring(body) end
726
+
727
+ local headers = {}
728
+ if type(resp.headers) == 'table' then
729
+ for k, v in pairs(resp.headers) do
730
+ headers[M.clean_header(k)] = M.clean_header(v)
731
+ end
732
+ end
733
+
734
+ headers['Server'] = headers['Server'] or opts.server_name or 'xnet-http'
735
+ headers['Connection'] = headers['Connection'] or (req.keep_alive and 'keep-alive' or 'close')
736
+
737
+ -- HSTS: ask compliant clients to use HTTPS for future requests. Only
738
+ -- meaningful over TLS, so the worker passes opts.hsts on HTTPS responses.
739
+ if opts.hsts and not headers['Strict-Transport-Security'] then
740
+ local hv = M.hsts_value(opts.hsts)
741
+ if hv then headers['Strict-Transport-Security'] = hv end
742
+ end
743
+
744
+ if file_path then
745
+ local f = io.open(file_path, 'rb')
746
+ if not f then
747
+ status = 404
748
+ body = 'file not found\n'
749
+ file_path = nil
750
+ else
751
+ local size = f:seek('end') or 0
752
+ f:close()
753
+ headers['Content-Length'] = tostring(size)
754
+ if not headers['Content-Type'] then
755
+ headers['Content-Type'] = resp.content_type or 'application/octet-stream'
756
+ end
757
+ end
758
+ end
759
+
760
+ if not file_path then
761
+ if body ~= '' and not headers['Content-Type'] then
762
+ headers['Content-Type'] = 'text/plain; charset=utf-8'
763
+ end
764
+
765
+ local compr = opts.compression
766
+ if compr and compr.enabled ~= false
767
+ and not headers['Content-Encoding']
768
+ and #body >= (compr.min_size or 256)
769
+ and compressible_content_type(headers['Content-Type']) then
770
+ local accept = req.headers and req.headers['accept-encoding']
771
+ local enc = M.select_encoding(accept)
772
+ if enc then
773
+ local cz = M.compress_body(body, enc, compr.level)
774
+ -- Only swap if compression actually shrinks the body. For very
775
+ -- small or random data, gzip headers + deflate framing can grow
776
+ -- it; in that case fall back to plain.
777
+ if cz and #cz < #body then
778
+ body = cz
779
+ headers['Content-Encoding'] = enc
780
+ headers['Vary'] = headers['Vary'] or 'Accept-Encoding'
781
+ end
782
+ end
783
+ end
784
+
785
+ headers['Content-Length'] = tostring(#body)
786
+ end
787
+
788
+ return status, body, headers, file_path
789
+ end
790
+
791
+ function M.send_response(conn, req, resp, opts)
792
+ local status, body, headers, file_path = M.normalize_response(req, resp, opts)
793
+ local reason = M.STATUS_TEXT[status] or 'Status'
794
+ local out = { string.format('HTTP/1.1 %d %s\r\n', status, reason) }
795
+ for k, v in pairs(headers) do
796
+ out[#out + 1] = M.clean_header(k) .. ': ' .. M.clean_header(v) .. '\r\n'
797
+ end
798
+ out[#out + 1] = '\r\n'
799
+ local header = table.concat(out)
800
+
801
+ if file_path and req.method ~= 'HEAD' then
802
+ if type(conn.send_file_response) == 'function' and conn:send_file_response(header, file_path) then
803
+ return true
804
+ end
805
+ local data = read_file(file_path)
806
+ if data then
807
+ conn:send_raw(header .. data)
808
+ return true
809
+ end
810
+ end
811
+
812
+ out = { header }
813
+ if req.method ~= 'HEAD' and not file_path then
814
+ out[#out + 1] = body
815
+ end
816
+ conn:send_raw(table.concat(out))
817
+ return true
818
+ end
819
+
820
+ function M.send_error(conn, status, message, opts)
821
+ local body = tostring(message or M.STATUS_TEXT[status] or 'error') .. '\n'
822
+ return M.send_response(conn, {
823
+ method = 'GET',
824
+ keep_alive = false,
825
+ }, {
826
+ status = status,
827
+ body = body,
828
+ headers = {
829
+ ['Content-Type'] = 'text/plain; charset=utf-8',
830
+ ['Connection'] = 'close',
831
+ },
832
+ }, opts)
833
+ end
834
+
835
+ return M