-
Notifications
You must be signed in to change notification settings - Fork 453
/
http.c
4848 lines (3852 loc) · 115 KB
/
http.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* HTTP routines for CUPS.
*
* Copyright © 2007-2019 by Apple Inc.
* Copyright © 1997-2007 by Easy Software Products, all rights reserved.
*
* This file contains Kerberos support code, copyright 2006 by
* Jelmer Vernooij.
*
* Licensed under Apache License v2.0. See the file "LICENSE" for more
* information.
*/
/*
* Include necessary headers...
*/
#include "cups-private.h"
#include "debug-internal.h"
#include <fcntl.h>
#include <math.h>
#ifdef _WIN32
# include <tchar.h>
#else
# include <signal.h>
# include <sys/time.h>
# include <sys/resource.h>
#endif /* _WIN32 */
#ifdef HAVE_POLL
# include <poll.h>
#endif /* HAVE_POLL */
# ifdef HAVE_LIBZ
# include <zlib.h>
# endif /* HAVE_LIBZ */
/*
* Local functions...
*/
static void http_add_field(http_t *http, http_field_t field, const char *value, int append);
#ifdef HAVE_LIBZ
static void http_content_coding_finish(http_t *http);
static void http_content_coding_start(http_t *http,
const char *value);
#endif /* HAVE_LIBZ */
static http_t *http_create(const char *host, int port,
http_addrlist_t *addrlist, int family,
http_encryption_t encryption,
int blocking, _http_mode_t mode);
#ifdef DEBUG
static void http_debug_hex(const char *prefix, const char *buffer,
int bytes);
#endif /* DEBUG */
static ssize_t http_read(http_t *http, char *buffer, size_t length);
static ssize_t http_read_buffered(http_t *http, char *buffer, size_t length);
static ssize_t http_read_chunk(http_t *http, char *buffer, size_t length);
static int http_send(http_t *http, http_state_t request,
const char *uri);
static ssize_t http_write(http_t *http, const char *buffer,
size_t length);
static ssize_t http_write_chunk(http_t *http, const char *buffer,
size_t length);
static off_t http_set_length(http_t *http);
static void http_set_timeout(int fd, double timeout);
static void http_set_wait(http_t *http);
#ifdef HAVE_SSL
static int http_tls_upgrade(http_t *http);
#endif /* HAVE_SSL */
/*
* Local globals...
*/
static const char * const http_fields[] =
{
"Accept-Language",
"Accept-Ranges",
"Authorization",
"Connection",
"Content-Encoding",
"Content-Language",
"Content-Length",
"Content-Location",
"Content-MD5",
"Content-Range",
"Content-Type",
"Content-Version",
"Date",
"Host",
"If-Modified-Since",
"If-Unmodified-since",
"Keep-Alive",
"Last-Modified",
"Link",
"Location",
"Range",
"Referer",
"Retry-After",
"Transfer-Encoding",
"Upgrade",
"User-Agent",
"WWW-Authenticate",
"Accept-Encoding",
"Allow",
"Server",
"Authentication-Info"
};
/*
* 'httpAcceptConnection()' - Accept a new HTTP client connection from the
* specified listening socket.
*
* @since CUPS 1.7/macOS 10.9@
*/
http_t * /* O - HTTP connection or @code NULL@ */
httpAcceptConnection(int fd, /* I - Listen socket file descriptor */
int blocking) /* I - 1 if the connection should be
blocking, 0 otherwise */
{
http_t *http; /* HTTP connection */
http_addrlist_t addrlist; /* Dummy address list */
socklen_t addrlen; /* Length of address */
int val; /* Socket option value */
/*
* Range check input...
*/
if (fd < 0)
return (NULL);
/*
* Create the client connection...
*/
memset(&addrlist, 0, sizeof(addrlist));
if ((http = http_create(NULL, 0, &addrlist, AF_UNSPEC,
HTTP_ENCRYPTION_IF_REQUESTED, blocking,
_HTTP_MODE_SERVER)) == NULL)
return (NULL);
/*
* Accept the client and get the remote address...
*/
addrlen = sizeof(http_addr_t);
if ((http->fd = accept(fd, (struct sockaddr *)&(http->addrlist->addr),
&addrlen)) < 0)
{
_cupsSetHTTPError(HTTP_STATUS_ERROR);
httpClose(http);
return (NULL);
}
http->hostaddr = &(http->addrlist->addr);
if (httpAddrLocalhost(http->hostaddr))
strlcpy(http->hostname, "localhost", sizeof(http->hostname));
else
httpAddrString(http->hostaddr, http->hostname, sizeof(http->hostname));
#ifdef SO_NOSIGPIPE
/*
* Disable SIGPIPE for this socket.
*/
val = 1;
setsockopt(http->fd, SOL_SOCKET, SO_NOSIGPIPE, CUPS_SOCAST &val, sizeof(val));
#endif /* SO_NOSIGPIPE */
/*
* Using TCP_NODELAY improves responsiveness, especially on systems
* with a slow loopback interface. Since we write large buffers
* when sending print files and requests, there shouldn't be any
* performance penalty for this...
*/
val = 1;
setsockopt(http->fd, IPPROTO_TCP, TCP_NODELAY, CUPS_SOCAST &val, sizeof(val));
#ifdef FD_CLOEXEC
/*
* Close this socket when starting another process...
*/
fcntl(http->fd, F_SETFD, FD_CLOEXEC);
#endif /* FD_CLOEXEC */
return (http);
}
/*
* 'httpAddCredential()' - Allocates and adds a single credential to an array.
*
* Use @code cupsArrayNew(NULL, NULL)@ to create a credentials array.
*
* @since CUPS 1.5/macOS 10.7@
*/
int /* O - 0 on success, -1 on error */
httpAddCredential(
cups_array_t *credentials, /* I - Credentials array */
const void *data, /* I - PEM-encoded X.509 data */
size_t datalen) /* I - Length of data */
{
http_credential_t *credential; /* Credential data */
if ((credential = malloc(sizeof(http_credential_t))) != NULL)
{
credential->datalen = datalen;
if ((credential->data = malloc(datalen)) != NULL)
{
memcpy(credential->data, data, datalen);
cupsArrayAdd(credentials, credential);
return (0);
}
free(credential);
}
return (-1);
}
/*
* 'httpBlocking()' - Set blocking/non-blocking behavior on a connection.
*/
void
httpBlocking(http_t *http, /* I - HTTP connection */
int b) /* I - 1 = blocking, 0 = non-blocking */
{
if (http)
{
http->blocking = b;
http_set_wait(http);
}
}
/*
* 'httpCheck()' - Check to see if there is a pending response from the server.
*/
int /* O - 0 = no data, 1 = data available */
httpCheck(http_t *http) /* I - HTTP connection */
{
return (httpWait(http, 0));
}
/*
* 'httpClearCookie()' - Clear the cookie value(s).
*
* @since CUPS 1.1.19/macOS 10.3@
*/
void
httpClearCookie(http_t *http) /* I - HTTP connection */
{
if (!http)
return;
if (http->cookie)
{
free(http->cookie);
http->cookie = NULL;
}
}
/*
* 'httpClearFields()' - Clear HTTP request fields.
*/
void
httpClearFields(http_t *http) /* I - HTTP connection */
{
http_field_t field; /* Current field */
DEBUG_printf(("httpClearFields(http=%p)", (void *)http));
if (http)
{
memset(http->_fields, 0, sizeof(http->fields));
for (field = HTTP_FIELD_ACCEPT_LANGUAGE; field < HTTP_FIELD_MAX; field ++)
{
if (http->fields[field] && http->fields[field] != http->_fields[field])
free(http->fields[field]);
http->fields[field] = NULL;
}
if (http->mode == _HTTP_MODE_CLIENT)
{
if (http->hostname[0] == '/')
httpSetField(http, HTTP_FIELD_HOST, "localhost");
else
httpSetField(http, HTTP_FIELD_HOST, http->hostname);
}
http->expect = (http_status_t)0;
}
}
/*
* 'httpClose()' - Close an HTTP connection.
*/
void
httpClose(http_t *http) /* I - HTTP connection */
{
#ifdef HAVE_GSSAPI
OM_uint32 minor_status; /* Minor status code */
#endif /* HAVE_GSSAPI */
DEBUG_printf(("httpClose(http=%p)", (void *)http));
/*
* Range check input...
*/
if (!http)
return;
/*
* Close any open connection...
*/
_httpDisconnect(http);
/*
* Free memory used...
*/
httpAddrFreeList(http->addrlist);
if (http->cookie)
free(http->cookie);
#ifdef HAVE_GSSAPI
if (http->gssctx != GSS_C_NO_CONTEXT)
gss_delete_sec_context(&minor_status, &http->gssctx, GSS_C_NO_BUFFER);
if (http->gssname != GSS_C_NO_NAME)
gss_release_name(&minor_status, &http->gssname);
#endif /* HAVE_GSSAPI */
#ifdef HAVE_AUTHORIZATION_H
if (http->auth_ref)
AuthorizationFree(http->auth_ref, kAuthorizationFlagDefaults);
#endif /* HAVE_AUTHORIZATION_H */
httpClearFields(http);
if (http->authstring && http->authstring != http->_authstring)
free(http->authstring);
free(http);
}
/*
* 'httpCompareCredentials()' - Compare two sets of X.509 credentials.
*
* @since CUPS 2.0/OS 10.10@
*/
int /* O - 1 if they match, 0 if they do not */
httpCompareCredentials(
cups_array_t *cred1, /* I - First set of X.509 credentials */
cups_array_t *cred2) /* I - Second set of X.509 credentials */
{
http_credential_t *temp1, *temp2; /* Temporary credentials */
for (temp1 = (http_credential_t *)cupsArrayFirst(cred1), temp2 = (http_credential_t *)cupsArrayFirst(cred2); temp1 && temp2; temp1 = (http_credential_t *)cupsArrayNext(cred1), temp2 = (http_credential_t *)cupsArrayNext(cred2))
if (temp1->datalen != temp2->datalen)
return (0);
else if (memcmp(temp1->data, temp2->data, temp1->datalen))
return (0);
return (temp1 == temp2);
}
/*
* 'httpConnect()' - Connect to a HTTP server.
*
* This function is deprecated - use @link httpConnect2@ instead.
*
* @deprecated@ @exclude all@
*/
http_t * /* O - New HTTP connection */
httpConnect(const char *host, /* I - Host to connect to */
int port) /* I - Port number */
{
return (httpConnect2(host, port, NULL, AF_UNSPEC,
HTTP_ENCRYPTION_IF_REQUESTED, 1, 30000, NULL));
}
/*
* 'httpConnect2()' - Connect to a HTTP server.
*
* @since CUPS 1.7/macOS 10.9@
*/
http_t * /* O - New HTTP connection */
httpConnect2(
const char *host, /* I - Host to connect to */
int port, /* I - Port number */
http_addrlist_t *addrlist, /* I - List of addresses or @code NULL@ to lookup */
int family, /* I - Address family to use or @code AF_UNSPEC@ for any */
http_encryption_t encryption, /* I - Type of encryption to use */
int blocking, /* I - 1 for blocking connection, 0 for non-blocking */
int msec, /* I - Connection timeout in milliseconds, 0 means don't connect */
int *cancel) /* I - Pointer to "cancel" variable */
{
http_t *http; /* New HTTP connection */
DEBUG_printf(("httpConnect2(host=\"%s\", port=%d, addrlist=%p, family=%d, encryption=%d, blocking=%d, msec=%d, cancel=%p)", host, port, (void *)addrlist, family, encryption, blocking, msec, (void *)cancel));
/*
* Create the HTTP structure...
*/
if ((http = http_create(host, port, addrlist, family, encryption, blocking,
_HTTP_MODE_CLIENT)) == NULL)
return (NULL);
/*
* Optionally connect to the remote system...
*/
if (msec == 0 || !httpReconnect2(http, msec, cancel))
return (http);
/*
* Could not connect to any known address - bail out!
*/
httpClose(http);
return (NULL);
}
/*
* 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
*
* This function is now deprecated. Please use the @link httpConnect2@ function
* instead.
*
* @deprecated@ @exclude all@
*/
http_t * /* O - New HTTP connection */
httpConnectEncrypt(
const char *host, /* I - Host to connect to */
int port, /* I - Port number */
http_encryption_t encryption) /* I - Type of encryption to use */
{
DEBUG_printf(("httpConnectEncrypt(host=\"%s\", port=%d, encryption=%d)",
host, port, encryption));
return (httpConnect2(host, port, NULL, AF_UNSPEC, encryption, 1, 30000,
NULL));
}
/*
* 'httpDelete()' - Send a DELETE request to the server.
*/
int /* O - Status of call (0 = success) */
httpDelete(http_t *http, /* I - HTTP connection */
const char *uri) /* I - URI to delete */
{
return (http_send(http, HTTP_STATE_DELETE, uri));
}
/*
* '_httpDisconnect()' - Disconnect a HTTP connection.
*/
void
_httpDisconnect(http_t *http) /* I - HTTP connection */
{
#ifdef HAVE_SSL
if (http->tls)
_httpTLSStop(http);
#endif /* HAVE_SSL */
httpAddrClose(NULL, http->fd);
http->fd = -1;
}
/*
* 'httpEncryption()' - Set the required encryption on the link.
*/
int /* O - -1 on error, 0 on success */
httpEncryption(http_t *http, /* I - HTTP connection */
http_encryption_t e) /* I - New encryption preference */
{
DEBUG_printf(("httpEncryption(http=%p, e=%d)", (void *)http, e));
#ifdef HAVE_SSL
if (!http)
return (0);
if (http->mode == _HTTP_MODE_CLIENT)
{
http->encryption = e;
if ((http->encryption == HTTP_ENCRYPTION_ALWAYS && !http->tls) ||
(http->encryption == HTTP_ENCRYPTION_NEVER && http->tls))
return (httpReconnect2(http, 30000, NULL));
else if (http->encryption == HTTP_ENCRYPTION_REQUIRED && !http->tls)
return (http_tls_upgrade(http));
else
return (0);
}
else
{
if (e == HTTP_ENCRYPTION_NEVER && http->tls)
return (-1);
http->encryption = e;
if (e != HTTP_ENCRYPTION_IF_REQUESTED && !http->tls)
return (_httpTLSStart(http));
else
return (0);
}
#else
if (e == HTTP_ENCRYPTION_ALWAYS || e == HTTP_ENCRYPTION_REQUIRED)
return (-1);
else
return (0);
#endif /* HAVE_SSL */
}
/*
* 'httpError()' - Get the last error on a connection.
*/
int /* O - Error code (errno) value */
httpError(http_t *http) /* I - HTTP connection */
{
if (http)
return (http->error);
else
return (EINVAL);
}
/*
* 'httpFieldValue()' - Return the HTTP field enumeration value for a field
* name.
*/
http_field_t /* O - Field index */
httpFieldValue(const char *name) /* I - String name */
{
int i; /* Looping var */
for (i = 0; i < HTTP_FIELD_MAX; i ++)
if (!_cups_strcasecmp(name, http_fields[i]))
return ((http_field_t)i);
return (HTTP_FIELD_UNKNOWN);
}
/*
* 'httpFlush()' - Flush data read from a HTTP connection.
*/
void
httpFlush(http_t *http) /* I - HTTP connection */
{
char buffer[8192]; /* Junk buffer */
int blocking; /* To block or not to block */
http_state_t oldstate; /* Old state */
DEBUG_printf(("httpFlush(http=%p), state=%s", (void *)http, httpStateString(http->state)));
/*
* Nothing to do if we are in the "waiting" state...
*/
if (http->state == HTTP_STATE_WAITING)
return;
/*
* Temporarily set non-blocking mode so we don't get stuck in httpRead()...
*/
blocking = http->blocking;
http->blocking = 0;
/*
* Read any data we can...
*/
oldstate = http->state;
while (httpRead2(http, buffer, sizeof(buffer)) > 0);
/*
* Restore blocking and reset the connection if we didn't get all of
* the remaining data...
*/
http->blocking = blocking;
if (http->state == oldstate && http->state != HTTP_STATE_WAITING &&
http->fd >= 0)
{
/*
* Didn't get the data back, so close the current connection.
*/
#ifdef HAVE_LIBZ
if (http->coding)
http_content_coding_finish(http);
#endif /* HAVE_LIBZ */
DEBUG_puts("1httpFlush: Setting state to HTTP_STATE_WAITING and closing.");
http->state = HTTP_STATE_WAITING;
#ifdef HAVE_SSL
if (http->tls)
_httpTLSStop(http);
#endif /* HAVE_SSL */
httpAddrClose(NULL, http->fd);
http->fd = -1;
}
}
/*
* 'httpFlushWrite()' - Flush data written to a HTTP connection.
*
* @since CUPS 1.2/macOS 10.5@
*/
int /* O - Bytes written or -1 on error */
httpFlushWrite(http_t *http) /* I - HTTP connection */
{
ssize_t bytes; /* Bytes written */
DEBUG_printf(("httpFlushWrite(http=%p) data_encoding=%d", (void *)http, http ? http->data_encoding : 100));
if (!http || !http->wused)
{
DEBUG_puts(http ? "1httpFlushWrite: Write buffer is empty." :
"1httpFlushWrite: No connection.");
return (0);
}
if (http->data_encoding == HTTP_ENCODING_CHUNKED)
bytes = http_write_chunk(http, http->wbuffer, (size_t)http->wused);
else
bytes = http_write(http, http->wbuffer, (size_t)http->wused);
http->wused = 0;
DEBUG_printf(("1httpFlushWrite: Returning %d, errno=%d.", (int)bytes, errno));
return ((int)bytes);
}
/*
* 'httpFreeCredentials()' - Free an array of credentials.
*/
void
httpFreeCredentials(
cups_array_t *credentials) /* I - Array of credentials */
{
http_credential_t *credential; /* Credential */
for (credential = (http_credential_t *)cupsArrayFirst(credentials);
credential;
credential = (http_credential_t *)cupsArrayNext(credentials))
{
cupsArrayRemove(credentials, credential);
free((void *)credential->data);
free(credential);
}
cupsArrayDelete(credentials);
}
/*
* 'httpGet()' - Send a GET request to the server.
*/
int /* O - Status of call (0 = success) */
httpGet(http_t *http, /* I - HTTP connection */
const char *uri) /* I - URI to get */
{
return (http_send(http, HTTP_STATE_GET, uri));
}
/*
* 'httpGetActivity()' - Get the most recent activity for a connection.
*
* The return value is the time in seconds of the last read or write.
*
* @since CUPS 2.0/OS 10.10@
*/
time_t /* O - Time of last read or write */
httpGetActivity(http_t *http) /* I - HTTP connection */
{
return (http ? http->activity : 0);
}
/*
* 'httpGetAuthString()' - Get the current authorization string.
*
* The authorization string is set by @link cupsDoAuthentication@ and
* @link httpSetAuthString@. Use @link httpGetAuthString@ to retrieve the
* string to use with @link httpSetField@ for the
* @code HTTP_FIELD_AUTHORIZATION@ value.
*
* @since CUPS 1.3/macOS 10.5@
*/
char * /* O - Authorization string */
httpGetAuthString(http_t *http) /* I - HTTP connection */
{
if (http)
return (http->authstring);
else
return (NULL);
}
/*
* 'httpGetBlocking()' - Get the blocking/non-block state of a connection.
*
* @since CUPS 1.2/macOS 10.5@
*/
int /* O - 1 if blocking, 0 if non-blocking */
httpGetBlocking(http_t *http) /* I - HTTP connection */
{
return (http ? http->blocking : 0);
}
/*
* 'httpGetContentEncoding()' - Get a common content encoding, if any, between
* the client and server.
*
* This function uses the value of the Accepts-Encoding HTTP header and must be
* called after receiving a response from the server or a request from the
* client. The value returned can be use in subsequent requests (for clients)
* or in the response (for servers) in order to compress the content stream.
*
* @since CUPS 1.7/macOS 10.9@
*/
const char * /* O - Content-Coding value or
@code NULL@ for the identity
coding. */
httpGetContentEncoding(http_t *http) /* I - HTTP connection */
{
#ifdef HAVE_LIBZ
if (http && http->fields[HTTP_FIELD_ACCEPT_ENCODING])
{
int i; /* Looping var */
char temp[HTTP_MAX_VALUE], /* Copy of Accepts-Encoding value */
*start, /* Start of coding value */
*end; /* End of coding value */
double qvalue; /* "qvalue" for coding */
struct lconv *loc = localeconv(); /* Locale data */
static const char * const codings[] =
{ /* Supported content codings */
"deflate",
"gzip",
"x-deflate",
"x-gzip"
};
strlcpy(temp, http->fields[HTTP_FIELD_ACCEPT_ENCODING], sizeof(temp));
for (start = temp; *start; start = end)
{
/*
* Find the end of the coding name...
*/
qvalue = 1.0;
end = start;
while (*end && *end != ';' && *end != ',' && !isspace(*end & 255))
end ++;
if (*end == ';')
{
/*
* Grab the qvalue as needed...
*/
if (!strncmp(end, ";q=", 3))
qvalue = _cupsStrScand(end + 3, NULL, loc);
/*
* Skip past all attributes...
*/
*end++ = '\0';
while (*end && *end != ',' && !isspace(*end & 255))
end ++;
}
else if (*end)
*end++ = '\0';
while (*end && isspace(*end & 255))
end ++;
/*
* Check value if it matches something we support...
*/
if (qvalue <= 0.0)
continue;
for (i = 0; i < (int)(sizeof(codings) / sizeof(codings[0])); i ++)
if (!strcmp(start, codings[i]))
return (codings[i]);
}
}
#endif /* HAVE_LIBZ */
return (NULL);
}
/*
* 'httpGetCookie()' - Get any cookie data from the response.
*
* @since CUPS 1.1.19/macOS 10.3@
*/
const char * /* O - Cookie data or @code NULL@ */
httpGetCookie(http_t *http) /* I - HTTP connection */
{
return (http ? http->cookie : NULL);
}
/*
* 'httpGetEncryption()' - Get the current encryption mode of a connection.
*
* This function returns the encryption mode for the connection. Use the
* @link httpIsEncrypted@ function to determine whether a TLS session has
* been established.
*
* @since CUPS 2.0/OS 10.10@
*/
http_encryption_t /* O - Current encryption mode */
httpGetEncryption(http_t *http) /* I - HTTP connection */
{
return (http ? http->encryption : HTTP_ENCRYPTION_IF_REQUESTED);
}
/*
* 'httpGetExpect()' - Get the value of the Expect header, if any.
*
* Returns @code HTTP_STATUS_NONE@ if there is no Expect header, otherwise
* returns the expected HTTP status code, typically @code HTTP_STATUS_CONTINUE@.
*
* @since CUPS 1.7/macOS 10.9@
*/
http_status_t /* O - Expect: status, if any */
httpGetExpect(http_t *http) /* I - HTTP connection */
{
if (!http)
return (HTTP_STATUS_ERROR);
else
return (http->expect);
}
/*
* 'httpGetFd()' - Get the file descriptor associated with a connection.
*
* @since CUPS 1.2/macOS 10.5@
*/
int /* O - File descriptor or -1 if none */
httpGetFd(http_t *http) /* I - HTTP connection */
{
return (http ? http->fd : -1);
}
/*
* 'httpGetField()' - Get a field value from a request/response.
*/
const char * /* O - Field value */
httpGetField(http_t *http, /* I - HTTP connection */
http_field_t field) /* I - Field to get */
{
if (!http || field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX)
return (NULL);
else if (http->fields[field])
return (http->fields[field]);
else
return ("");
}
/*
* 'httpGetKeepAlive()' - Get the current Keep-Alive state of the connection.
*
* @since CUPS 2.0/OS 10.10@
*/
http_keepalive_t /* O - Keep-Alive state */
httpGetKeepAlive(http_t *http) /* I - HTTP connection */
{
return (http ? http->keep_alive : HTTP_KEEPALIVE_OFF);
}
/*
* 'httpGetLength()' - Get the amount of data remaining from the
* content-length or transfer-encoding fields.
*
* This function is deprecated and will not return lengths larger than
* 2^31 - 1; use httpGetLength2() instead.
*
* @deprecated@ @exclude all@
*/
int /* O - Content length */
httpGetLength(http_t *http) /* I - HTTP connection */
{
/*
* Get the read content length and return the 32-bit value.
*/
if (http)
{
httpGetLength2(http);
return (http->_data_remaining);
}
else
return (-1);
}
/*
* 'httpGetLength2()' - Get the amount of data remaining from the
* content-length or transfer-encoding fields.
*
* This function returns the complete content length, even for