ksgi

Форк
0
/
khttp_parse.3 
928 строк · 21.9 Кб
1
.\"	$Id$
2
.\"
3
.\" Copyright (c) 2014--2017 Kristaps Dzonsons <kristaps@bsd.lv>
4
.\" Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org>
5
.\"
6
.\" Permission to use, copy, modify, and distribute this software for any
7
.\" purpose with or without fee is hereby granted, provided that the above
8
.\" copyright notice and this permission notice appear in all copies.
9
.\"
10
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
.\"
18
.Dd $Mdocdate$
19
.Dt KHTTP_PARSE 3
20
.Os
21
.Sh NAME
22
.Nm khttp_parse ,
23
.Nm khttp_parsex
24
.Nd parse a CGI instance for kcgi
25
.Sh LIBRARY
26
.Lb libkcgi
27
.Sh SYNOPSIS
28
.In sys/types.h
29
.In stdarg.h
30
.In stdint.h
31
.In kcgi.h
32
.Ft "enum kcgi_err"
33
.Fo khttp_parse
34
.Fa "struct kreq *req"
35
.Fa "const struct kvalid *keys"
36
.Fa "size_t keysz"
37
.Fa "const char *const *pages"
38
.Fa "size_t pagesz"
39
.Fa "size_t defpage"
40
.Fc
41
.Ft "enum kcgi_err"
42
.Fo khttp_parsex
43
.Fa "struct kreq *req"
44
.Fa "const struct kmimemap *suffixes"
45
.Fa "const char *const *mimes"
46
.Fa "size_t mimesz"
47
.Fa "const struct kvalid *keys"
48
.Fa "size_t keysz"
49
.Fa "const char *const *pages"
50
.Fa "size_t pagesz"
51
.Fa "size_t defmime"
52
.Fa "size_t defpage"
53
.Fa "void *arg"
54
.Fa "void (*argfree)(void *arg)"
55
.Fa "unsigned int debugging"
56
.Fa "const struct kopts *opts"
57
.Fc
58
.Vt extern const char *const kmimetypes[KMIME__MAX];
59
.Vt extern const char *const khttps[KHTTP__MAX];
60
.Vt extern const char *const kschemes[KSCHEME__MAX];
61
.Vt extern const char *const kmethods[KMETHOD__MAX];
62
.Vt extern const struct kmimemap ksuffixmap[];
63
.Vt extern const char *const ksuffixes[KMIME__MAX];
64
.Sh DESCRIPTION
65
The
66
.Fn khttp_parse
67
and
68
.Fn khttp_parsex
69
functions parse and validate input and the HTTP environment
70
(compression, paths, MIME types, and so on).
71
They are the central functions in the
72
.Xr kcgi 3
73
library, parsing and validating key-value form (query string, message
74
body, cookie) data and opaque message bodies.
75
.Pp
76
They must be matched by
77
.Xr khttp_free 3
78
if and only if the return value is
79
.Dv KCGI_OK .
80
Otherwise, resources are internally freed.
81
.Pp
82
The collective arguments are as follows:
83
.Bl -tag -width Ds
84
.It Fa arg
85
A pointer to private application data.
86
It is not touched unless
87
.Fa argfree
88
is provided.
89
.It Fa argfree
90
Function invoked with
91
.Fa arg
92
by the child process starting to parse untrusted network data.
93
This makes sure that no unnecessary data is leaked into the child.
94
.It Fa debugging
95
This bit-field enables debugging of the underlying parse and/or write
96
routines.
97
It may have
98
.Dv KREQ_DEBUG_WRITE
99
for writes and
100
.Dv KREQ_DEBUG_READ_BODY
101
for the pre-parsed body.
102
Debugging messages to
103
.Xr kutil_info 3
104
consist of the process ID followed by
105
.Qq -tx
106
or
107
.Qq -rx
108
for writing or reading, a colon and space, then the logged data.
109
A newline will flush the existing line, as well reaching 80 characters.
110
If flushed at 80 characters and not a newline, an ellipsis will follow
111
the line.
112
The total logged bytes will be emitted at the end of all reads or
113
writes.
114
.It Fa defmime
115
If no MIME type is specified (that is, there's no suffix to the
116
page request), use this index in the
117
.Fa mimes
118
array.
119
.It Fa defpage
120
If no page was specified (e.g., the default landing page), this is
121
provided as the requested page index.
122
.It Fa keys
123
An optional array of input and validation fields or
124
.Dv NULL .
125
.It Fa keysz
126
The number of elements in
127
.Fa keys .
128
.It Fa mimesz
129
The number of elements in
130
.Fa mimes .
131
Also the MIME index used if no MIME type was matched.
132
This differs from
133
.Fa defmime ,
134
which is used if there is no MIME suffix at all.
135
.It Fa mimes
136
An array of MIME types (e.g.,
137
.Dq text/html ) ,
138
mapped into a MIME index during MIME body parsing.
139
This relates both to pages and input fields with a body type.
140
Any array should include at least
141
.Li text/plain ,
142
as this is the default content type for MIME documents.
143
.It Fa opts
144
Tunable options regarding socket buffer sizes and so on.
145
If set to
146
.Dv NULL ,
147
meaningful defaults are used.
148
.It Fa pages
149
An array of recognised pathnames.
150
When pathnames are parsed, they're matched to indices in this array.
151
.It Fa pagesz
152
The number of pages in
153
.Fa pages .
154
Also used if the requested page was not in
155
.Fa pages .
156
.It Fa req
157
This structure is cleared and filled with input fields and HTTP context
158
parsed from the CGI environment.
159
It is the main structure carried around in a
160
.Xr kcgi 3
161
application.
162
.It Fa suffixes
163
Define the MIME type (suffix) mapping.
164
.El
165
.Pp
166
The first form,
167
.Fn khttp_parse ,
168
is for applications using the system-recognised MIME types.
169
This should work well enough for most applications.
170
It is equivalent to invoking the second form,
171
.Fn khttp_parsex ,
172
as follows:
173
.Bd -literal -offset indent
174
khttp_parsex(req, ksuffixmap,
175
  kmimetypes, KMIME__MAX, keys, keysz,
176
  pages, pagesz, KMIME_TEXT_HTML,
177
  defpage, NULL, NULL, 0, NULL);
178
.Ed
179
.Ss Types
180
A
181
.Vt "struct kreq"
182
object is filled in by
183
.Fn khttp_parse
184
and
185
.Fn khttp_parsex .
186
It consists of the following fields:
187
.Bl -tag -width Ds
188
.It Vt "void *" Ns Va arg
189
Private application data.
190
This is set during
191
.Fn khttp_parse .
192
.It Vt "enum kauth" Va auth
193
Type of
194
.Dq managed
195
HTTP authorisation performed by the web server according to the
196
.Ev AUTH_TYPE
197
header variable, if any.
198
This is
199
.Dv KAUTH_DIGEST
200
for the
201
.Ev AUTH_TYPE
202
of
203
.Qq digest ,
204
.Dv KAUTH_BASIC
205
for
206
.Qq basic ,
207
.Dv KAUTH_BEARER
208
for
209
.Qq bearer ,
210
.Dv KAUTH_UNKNOWN
211
for other values of
212
.Ev AUTH_TYPE ,
213
or
214
.Dv KAUTH_NONE
215
if
216
.Ev AUTH_TYPE
217
is not set.
218
See the
219
.Va rawauth
220
field for raw (i.e., not processed by the web server) authorisation
221
requests.
222
.It Vt "struct kpair **" Ns Va cookiemap
223
An array of
224
.Fa keysz
225
singly linked lists of elements of the
226
.Va cookies
227
array.
228
If
229
.Va cookie Ns -> Ns Va key
230
is equal to one of the entries of
231
.Fa keys
232
and
233
.Va cookie Ns -> Ns Va state
234
is
235
.Dv KPAIR_VALID
236
or
237
.Dv KPAIR_UNCHECKED ,
238
the cookie is added to the list
239
.Va cookiemap Ns Bq Va cookie Ns -> Ns Va keypos .
240
Empty lists are
241
.Dv NULL .
242
If a list contains more than one cookie,
243
.Va cookie Ns -> Ns Va next
244
points to the next cookie.
245
For the last cookie in a list,
246
.Va cookie Ns -> Ns Va next
247
is NULL.
248
.It Vt "struct kpair **" Ns Va cookienmap
249
Similar to
250
.Va cookiemap ,
251
except that it contains the cookies where
252
.Va cookie Ns -> Ns Va state
253
is
254
.Dv KPAIR_INVALID .
255
.It Vt "struct kpair *" Ns Va cookies
256
Key-value pairs read from request cookies found in the
257
.Ev HTTP_COOKIE
258
header variable, or
259
.Dv NULL
260
if
261
.Va cookiesz
262
is 0.
263
See
264
.Va fields
265
for key-value pairs from the request query string or message body.
266
.It Vt size_t Va cookiesz
267
The size of the
268
.Va cookies
269
array.
270
.It Vt "struct kpair **" Ns Va fieldmap
271
Similar to
272
.Va cookiemap ,
273
except that the lists contain elements of the
274
.Va fields
275
array.
276
.It Vt "struct kpair **" Ns Va fieldnmap
277
Similar to
278
.Va fieldmap ,
279
except that it contains the fields where
280
.Va field Ns -> Ns Va state
281
is
282
.Dv KPAIR_INVALID .
283
.It Vt "struct kpair *" Ns Va fields
284
Key-value pairs read from the
285
.Ev QUERY_STRING
286
header variable and from the message body, or
287
.Dv NULL
288
if
289
.Dv fieldsz
290
is 0.
291
See
292
.Va cookies
293
for key-value pairs from request cookies.
294
.It Vt size_t Va fieldsz
295
The number of elements in the
296
.Va fields
297
array.
298
.It Vt "char *" Ns Va fullpath
299
The full requested path as contained in the
300
.Ev PATH_INFO
301
header variable.
302
For example, requesting
303
.Qq https://bsd.lv/app.cgi/dir/file.html?q=v ,
304
where
305
.Qq app.cgi
306
is the CGI program, this value would be
307
.Pa /dir/file.html .
308
It is not guaranteed to start with a slash and it may be an empty
309
string.
310
.It Vt "char *" Ns Va host
311
The host name received in the
312
.Ev HTTP_HOST
313
header variable.
314
When using name-based virtual hosting, this is typically the virtual
315
host name specified by the client in the HTTP request, and it should
316
not be confused with the canonical DNS name of the host running the
317
web server.
318
For example, a request to
319
.Qq https://bsd.lv/app.cgi/file
320
would have a host of
321
.Qq bsd.lv .
322
If
323
.Ev HTTP_HOST
324
is not defined,
325
.Va host
326
is set to
327
.Qq localhost .
328
.It Vt "struct kdata *" Ns Va kdata
329
Internal data.
330
Should not be touched.
331
.It Vt "const struct kvalid *" Ns Va keys
332
Value passed to
333
.Fn khttp_parse .
334
.It Vt size_t Va keysz
335
Value passed to
336
.Fn khttp_parse .
337
.It Vt "enum kmethod" Va method
338
The
339
.Dv KMETHOD_ACL ,
340
.Dv KMETHOD_CONNECT ,
341
.Dv KMETHOD_COPY ,
342
.Dv KMETHOD_DELETE ,
343
.Dv KMETHOD_GET ,
344
.Dv KMETHOD_HEAD ,
345
.Dv KMETHOD_LOCK ,
346
.Dv KMETHOD_MKCALENDAR ,
347
.Dv KMETHOD_MKCOL ,
348
.Dv KMETHOD_MOVE ,
349
.Dv KMETHOD_OPTIONS ,
350
.Dv KMETHOD_POST ,
351
.Dv KMETHOD_PROPFIND ,
352
.Dv KMETHOD_PROPPATCH ,
353
.Dv KMETHOD_PUT ,
354
.Dv KMETHOD_REPORT ,
355
.Dv KMETHOD_TRACE ,
356
or
357
.Dv KMETHOD_UNLOCK
358
submission method obtained from the
359
.Ev REQUEST_METHOD
360
header variable.
361
If an unknown method was requested,
362
.Dv KMETHOD__MAX
363
is used.
364
If no method was specified, the default is
365
.Dv KMETHOD_GET .
366
.Pp
367
Applications will usually accept only
368
.Dv KMETHOD_GET
369
and
370
.Dv KMETHOD_POST ,
371
so be sure to emit a
372
.Dv KHTTP_405
373
status for undesired methods.
374
.It Vt size_t Va mime
375
The MIME type of the requested file as determined by its
376
.Pa suffix
377
matched to the
378
.Fa mimemap
379
map passed to
380
.Fn khttp_parsex
381
or the default
382
.Va kmimemap
383
if using
384
.Fn khttp_parse .
385
This defaults to the
386
.Va mimesz
387
value passed to
388
.Fn khttp_parsex
389
or the default
390
.Dv KMIME__MAX
391
if using
392
.Fn khttp_parse
393
when no suffix is specified or when the suffix is specified but not known.
394
.It Vt size_t Va page
395
The page index found by looking up
396
.Va pagename
397
in the
398
.Fa pages
399
array.
400
If
401
.Va pagename
402
is not found in
403
.Fa pages ,
404
.Fa pagesz
405
is used; if
406
.Va pagename
407
is empty,
408
.Fa defpage
409
is used.
410
.It Vt "char *" Ns Va pagename
411
The first component of
412
.Va fullpath
413
or an empty string if there is none.
414
It is compared to the elements of the
415
.Fa pages
416
array to determine which
417
.Va page
418
it corresponds to.
419
For example, for a
420
.Va fullpath
421
of
422
.Qq /dir/file.html
423
this component corresponds to
424
.Pa dir .
425
For
426
.Qq /file.html ,
427
it's
428
.Pa file .
429
.It Vt "char *" Ns Va path
430
The middle part of
431
.Va fullpath ,
432
after stripping
433
.Va pagename Ns /
434
at the beginning and
435
.Pf \&. Va suffix
436
at the end, or an empty string if there is none.
437
For example, if the
438
.Va fullpath
439
is
440
.Pa bar/baz.html ,
441
this component is
442
.Pa baz .
443
.It Vt "char *" Ns Va pname
444
The script name received in the
445
.Ev SCRIPT_NAME
446
header variable.
447
For example, for a request to a CGI program
448
.Pa /var/www/cgi-bin/app.cgi
449
mapped by the web server from
450
.Qq https://bsd.lv/app.cgi/file ,
451
this would be
452
.Pa app.cgi .
453
This may not reflect a file system entity and it may be an empty string.
454
.It Vt uint16_t Va port
455
The server's receiving TCP port according to the
456
.Ev SERVER_PORT
457
header variable, or 80 if that is not defined or an invalid number.
458
.It Vt "struct khttpauth" Va rawauth
459
The raw authorization request according to the
460
.Ev HTTP_AUTHORIZATION
461
header variable passed by the web server.
462
This is only set if the web server is not managing authorisation itself.
463
.It Vt "char *" Ns Va remote
464
The string form of the client's IPv4 or IPv6 address taken from the
465
.Ev REMOTE_ADDR
466
header variable, or
467
.Qq 127.0.0.1
468
if that is not defined.
469
The address format of the string is not checked.
470
.It Vt "struct khead *" Ns Va reqmap Ns Bq Dv KREQU__MAX
471
Mapping of
472
.Vt "enum krequ"
473
enumeration values to
474
.Va reqs
475
parsed from the input stream.
476
.It Vt "struct khead *" Ns Va reqs
477
List of all HTTP request headers, known via
478
.Vt "enum krequ"
479
and not known, parsed from the input stream, or
480
.Dv NULL
481
if
482
.Va reqsz
483
is 0.
484
.It Vt size_t Va reqsz
485
Number of request headers in
486
.Va reqs .
487
.It Vt "enum kscheme" Va scheme
488
The access scheme according to the
489
.Ev HTTPS
490
header variable, either
491
.Dv KSCHEME_HTTPS
492
if
493
.Ev HTTPS
494
is set and equal to the string
495
.Qq on
496
or
497
.Dv KSCHEME_HTTP
498
otherwise.
499
.It Vt "char *" Ns Va suffix
500
The suffix part of the last component of
501
.Va fullpath
502
or an empty string if there is none.
503
For example, if the
504
.Va fullpath
505
is
506
.Pa /bar/baz.html ,
507
this component is
508
.Pa html .
509
See the
510
.Va mime
511
field for the MIME type parsed from the suffix.
512
.El
513
.Pp
514
The application may optionally define
515
.Vt keys
516
provided to
517
.Fn khttp_parse
518
and
519
.Fn khttp_parsex
520
as an array of
521
.Vt "struct kvalid" .
522
This structure is central to the validation of input data.
523
It consists of the following fields:
524
.Bl -tag -width Ds
525
.It Vt "const char *" Ns Va name
526
The field name, i.e., how it appears in the HTML form input name.
527
This cannot be
528
.Dv NULL .
529
If the field name is an empty string and the HTTP message consists of an
530
opaque body (and not key-value pairs), then that field will be used to
531
validate the HTTP message body.
532
This is useful for
533
.Dv KMETHOD_PUT
534
style requests.
535
.It Vt "int (*)(struct kpair *)" Va valid
536
A validation function returning non-zero if parsing and validation
537
succeed or 0 otherwise.
538
If it is
539
.Dv NULL ,
540
then no validation is performed, the data is considered as valid,
541
and it is bucketed into
542
.Va cookiemap
543
or
544
.Va fieldmap
545
as such.
546
.Pp
547
User-defined
548
.Fa valid
549
functions usually set the
550
.Va type
551
and
552
.Va parsed
553
fields in the key-value pair.
554
When working with binary data or with a key that can take different
555
data types, it is acceptable for a validation function to set the
556
.Va type
557
to
558
.Dv KPAIR__MAX
559
and for the application to ignore the
560
.Va parsed
561
field and to work directly with
562
.Va val
563
and
564
.Va valsz .
565
.Pp
566
The validation function is allowed to allocate new memory for
567
.Va val :
568
if the
569
.Va val
570
pointer changes during validation, the memory pointed to after validation
571
will be freed with
572
.Xr free 3
573
after the data is passed out of the sandbox.
574
.Pp
575
These functions are invoked from within a system-specific sandbox
576
that may not allow some system calls, for example opening files or
577
sockets.
578
In other words, validation functions should only do pure computation.
579
.El
580
.Pp
581
The
582
.Vt "struct kpair"
583
structure presents the user with fields parsed from input and (possibly)
584
matched to the
585
.Fa keys
586
variable passed to
587
.Fn khttp_parse
588
and
589
.Fn khttp_parsex .
590
It is also passed to the validation function to be filled in.
591
In this case, the MIME-related fields are already filled in and may be
592
examined to determine the method of validation.
593
This is useful when validating opaque message bodies.
594
.Bl -tag -width Ds
595
.It Vt "char *" Ns Va ctype
596
The value's MIME content type (e.g.,
597
.Li image/jpeg ) ,
598
or
599
an empty string if not defined.
600
.It Vt "size_t" Va ctypepos
601
If
602
.Va ctype
603
is not
604
.Dv NULL ,
605
it is looked up in the
606
.Fa mimes
607
parameter passed to
608
.Fn khttp_parsex
609
or
610
.Va ksuffixmap
611
if using
612
.Fn khttp_parse .
613
If found, it is set to the appropriate index.
614
Otherwise, it's
615
.Fa mimesz .
616
.It Vt "char *" Ns Va file
617
The value's MIME source filename or
618
an empty string
619
if not defined.
620
.It Vt "char *" Ns Va key
621
The NUL-terminated key (input) name.
622
If the HTTP message body is opaque (e.g.,
623
.Dv KMETHOD_PUT ) ,
624
then an empty-string key is cooked up.
625
The key may contain an arbitrary sequence of non-NUL bytes, even
626
non-ASCII bytes, control characters, and shell metacharacters.
627
.It Vt "size_t" Va keypos
628
If found in the
629
.Fa keys
630
array passed to
631
.Fn khttp_parse ,
632
the index of the matching key.
633
Otherwise
634
.Fa keysz .
635
.It Vt "struct kpair *" Ns Va next
636
In a cookie or field map,
637
.Fa next
638
points to the next parsed key-value pair with the same
639
.Fa key
640
name.
641
This occurs most often in HTML checkbox forms, where many fields may
642
have the same name.
643
.It Vt "union parsed" Va parsed
644
The parsed, validated value.
645
These may be integer in
646
.Va i ,
647
for a 64-bit signed integer; a string
648
.Va s ,
649
for a NUL-termianted character string; or a double
650
.Va d ,
651
for a double-precision floating-point number.
652
This is intentionally basic because the resulting data must be reliably
653
passed from the parsing context back into the web application.
654
.It Vt "enum kpairstate" Va state
655
The validation state:
656
.Dv KPAIR_VALID
657
if the pair was successfully validated by a validation function,
658
.Dv KPAIR_INVALID
659
if a validation function was invoked but failed, or
660
.Dv KPAIR_UNCHECKED
661
if no validation function is defined for this
662
.Va key .
663
.It Vt "enum kpairtype" Va type
664
If parsed, the type of data in
665
.Fa parsed ,
666
otherwise
667
.Dv KFIELD__MAX .
668
.It Vt "char *" Ns Va val
669
The (input) value, which may contain an arbitrary sequence of bytes,
670
even NUL bytes, non-ASCII bytes, control characters, and shell
671
metacharacters.
672
The byte following the end of the array,
673
.Va val Ns Va [ valsz ] ,
674
is always guaranteed to be NUL.
675
The validation function may modify the contents.
676
For example, for integer numbers and e-mail adresses, trailing
677
whitespace may be replaced with NUL bytes.
678
.It Vt "size_t" Va valsz
679
The length of the
680
.Va val
681
buffer in bytes.
682
It is not a string length.
683
.It Vt "char *" Ns Va xcode
684
The value's MIME content transfer encoding (e.g.,
685
.Li base64 ) ,
686
or an empty string if not defined.
687
.El
688
.Pp
689
The
690
.Vt struct khttpauth
691
structure holds authorisation data if passed by the server.
692
The specific fields are as follows.
693
.Bl -tag -width Ds
694
.It Vt "enum kauth" Va type
695
If no data was passed by the server, the
696
.Va type
697
value is
698
.Dv KAUTH_NONE .
699
Otherwise it's
700
.Dv KAUTH_BASIC ,
701
.Dv KAUTH_BEARER ,
702
or
703
.Dv KAUTH_DIGEST .
704
.Dv KAUTH_UNKNOWN
705
signals that the authorisation type was not recognised.
706
.It Vt "int" Va authorised
707
For
708
.Dv KAUTH_BASIC ,
709
.Dv KAUTH_BEARER ,
710
or
711
.Dv KAUTH_DIGEST
712
authorisation, this field indicates whether all required values were
713
specified for the application to perform authorisation.
714
.It Vt "char *" Ns Va digest
715
An MD5 digest of
716
.Ev REQUEST_METHOD ,
717
.Ev SCRIPT_NAME ,
718
.Ev PATH_INFO ,
719
header variables and the request body.
720
It is not a NUL-terminated string, but an array of exactly
721
.Dv MD5_DIGEST_LENGTH
722
bytes.
723
Only filled in when
724
.Ev HTTP_AUTHORIZATION
725
is
726
.Qq digest
727
and
728
.Va authorised
729
is non-zero.
730
Otherwise, it remains
731
.Dv NULL .
732
Used in
733
.Xr khttpdigest_validatehash 3 .
734
.It Va d
735
An anonymous union containing parsed fields per type:
736
.Vt "struct khttpbasic" Va basic
737
for
738
.Dv KAUTH_BASIC
739
or
740
.Dv KAUTH_BEARER ,
741
or
742
.Vt "struct khttpdigest" Va digest
743
for
744
.Dv KAUTH_DIGEST .
745
.El
746
.Pp
747
If the field for an HTTP authorisation request is
748
.Dv KAUTH_BASIC
749
or
750
.Dv KAUTH_BEARER ,
751
it will consist of the following for its parsed entities in its
752
.Vt struct khttpbasic
753
structure:
754
.Bl -tag -width Ds
755
.It Va response
756
The hashed and encoded response string for
757
.Dv KAUTH_BASIC ,
758
or an opaque string for
759
.Dv KAUTH_BEARER .
760
.El
761
.Pp
762
If the field for an HTTP authorisation request is
763
.Dv KAUTH_DIGEST ,
764
it will consist of the following in its
765
.Vt struct khttpdigest
766
structure:
767
.Bl -tag -width Ds
768
.It Va alg
769
The encoding algorithm, parsed from the possible
770
.Li MD5
771
or
772
.Li MD5-Sess
773
values.
774
.It Va qop
775
The quality of protection algorithm, which may be unspecified,
776
.Li Auth
777
or
778
.Li Auth-Init .
779
.It Va user
780
The user coordinating the request.
781
.It Va uri
782
The URI for which the request is designated.
783
.Pq This must match the request URI .
784
.It Va realm
785
The request realm.
786
.It Va nonce
787
The server-generated nonce value.
788
.It Va cnonce
789
The (optional) client-generated nonce value.
790
.It Va response
791
The hashed and encoded response string, which entangled fields depending
792
on algorithm and quality of protection.
793
.It Va count
794
The (optional)
795
.Va cnonce
796
counter.
797
.It Va opaque
798
The (optional) opaque string requested by the server.
799
.El
800
.Pp
801
The
802
.Vt struct kopts
803
structure consists of tunables for network performance.
804
You probably don't want to use these unless you really know what you're
805
doing!
806
.Bl -tag -width Ds
807
.It Va sndbufsz
808
The size of the output buffer.
809
The output buffer is a heap-allocated region into which writes (via
810
.Xr khttp_write 3
811
and
812
.Xr khttp_head 3 )
813
are buffered instead of being flushed directly to the wire.
814
The buffer is flushed when it is full, when
815
the HTTP headers are flushed, and when
816
.Xr khttp_free 3
817
is invoked.
818
If the buffer size is zero, writes are flushed immediately to the wire.
819
If the buffer size is less than zero, it is filled with a meaningful
820
default.
821
.El
822
.Pp
823
Lastly, the
824
.Vt struct khead
825
structure holds parsed HTTP headers.
826
.Bl -tag -width Ds
827
.It Va key
828
Holds the HTTP header name.
829
This is not the CGI header name (e.g.,
830
.Li HTTP_COOKIE ) ,
831
but the reconstituted HTTP name (e.g.,
832
.Li Coookie ) .
833
.It Va val
834
The opaque header value, which may be an empty string.
835
.El
836
.Ss Variables
837
A number of variables are defined
838
.In kcgi.h
839
to simplify invocations of the
840
.Fn khttp_parse
841
family.
842
Applications are strongly suggested to use these variables (and
843
associated enumerations) in
844
.Fn khttp_parse
845
instead of overriding them with hand-rolled sets in
846
.Fn khttp_parsex .
847
.Bl -tag -width Ds
848
.It Va kmimetypes
849
Indexed list of common MIME types, for example,
850
.Dq text/html
851
and
852
.Dq application/json .
853
Corresponds to
854
.Vt enum kmime
855
.Vt enum khttp .
856
.It Va khttps
857
Indexed list of HTTP status code and identifier, for example,
858
.Dq 200 OK .
859
Corresponds to
860
.Vt enum khttp .
861
.It Va kschemes
862
Indexed list of URL schemes, for example,
863
.Dq https
864
or
865
.Dq ftp .
866
Corresponds to
867
.Vt enum kscheme .
868
.It Va kmethods
869
Indexed list of HTTP methods, for example,
870
.Dq GET
871
and
872
.Dq POST .
873
Corresponds to
874
.Vt enum kmethod .
875
.It Va ksuffixmap
876
Map of MIME types defined in
877
.Vt enum kmime
878
to possible suffixes.
879
This array is terminated with a MIME type of
880
.Dv KMIME__MAX
881
and name
882
.Dv NULL .
883
.It Va ksuffixes
884
Indexed list of canonical suffixes for MIME types corresponding to
885
.Vt enum kmime .
886
This may be a
887
.Dv NULL
888
pointer for types that have no canonical suffix, for example.
889
.Dq application/octet-stream .
890
.El
891
.Sh RETURN VALUES
892
.Fn khttp_parse
893
and
894
.Fn khttp_parsex
895
return an error code:
896
.Bl -tag -width -Ds
897
.It Dv KCGI_OK
898
Success (not an error).
899
.It Dv KCGI_ENOMEM
900
Memory failure.
901
This can occur in many places: spawning a child, allocating memory,
902
creating sockets, etc.
903
.It Dv KCGI_ENFILE
904
Could not allocate file descriptors.
905
.It Dv KCGI_EAGAIN
906
Could not spawn a child.
907
.It Dv KCGI_FORM
908
Malformed data between parent and child whilst parsing an HTTP request.
909
(Internal system error.)
910
.It Dv KCGI_SYSTEM
911
Opaque operating system error.
912
.El
913
.Pp
914
On failure, the calling application should terminate as soon as possible.
915
Applications should
916
.Em not
917
try to write an HTTP 505 error or similar, but allow the web server to
918
handle the empty CGI response on its own.
919
.Sh SEE ALSO
920
.Xr kcgi 3 ,
921
.Xr khttp_free 3
922
.Sh AUTHORS
923
The
924
.Fn khttp_parse
925
and
926
.Fn khttp_parsex
927
functions were written by
928
.An Kristaps Dzonsons Aq Mt kristaps@bsd.lv .
929

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.