-
Notifications
You must be signed in to change notification settings - Fork 452
/
http-support.c
2698 lines (2244 loc) · 66.8 KB
/
http-support.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 support routines for CUPS.
*
* Copyright 2007-2019 by Apple Inc.
* Copyright 1997-2007 by Easy Software Products, all rights reserved.
*
* Licensed under Apache License v2.0. See the file "LICENSE" for more
* information.
*/
/*
* Include necessary headers...
*/
#include "cups-private.h"
#include "debug-internal.h"
#ifdef HAVE_DNSSD
# include <dns_sd.h>
# ifdef _WIN32
# include <io.h>
# elif defined(HAVE_POLL)
# include <poll.h>
# else
# include <sys/select.h>
# endif /* _WIN32 */
#elif defined(HAVE_AVAHI)
# include <avahi-client/client.h>
# include <avahi-client/lookup.h>
# include <avahi-common/simple-watch.h>
#endif /* HAVE_DNSSD */
/*
* Local types...
*/
typedef struct _http_uribuf_s /* URI buffer */
{
#ifdef HAVE_AVAHI
AvahiSimplePoll *poll; /* Poll state */
#endif /* HAVE_AVAHI */
char *buffer; /* Pointer to buffer */
size_t bufsize; /* Size of buffer */
int options; /* Options passed to _httpResolveURI */
const char *resource; /* Resource from URI */
const char *uuid; /* UUID from URI */
} _http_uribuf_t;
/*
* Local globals...
*/
static const char * const http_days[7] =/* Days of the week */
{
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
};
static const char * const http_months[12] =
{ /* Months of the year */
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
};
static const char * const http_states[] =
{ /* HTTP state strings */
"HTTP_STATE_ERROR",
"HTTP_STATE_WAITING",
"HTTP_STATE_OPTIONS",
"HTTP_STATE_GET",
"HTTP_STATE_GET_SEND",
"HTTP_STATE_HEAD",
"HTTP_STATE_POST",
"HTTP_STATE_POST_RECV",
"HTTP_STATE_POST_SEND",
"HTTP_STATE_PUT",
"HTTP_STATE_PUT_RECV",
"HTTP_STATE_DELETE",
"HTTP_STATE_TRACE",
"HTTP_STATE_CONNECT",
"HTTP_STATE_STATUS",
"HTTP_STATE_UNKNOWN_METHOD",
"HTTP_STATE_UNKNOWN_VERSION"
};
/*
* Local functions...
*/
static const char *http_copy_decode(char *dst, const char *src,
int dstsize, const char *term,
int decode);
static char *http_copy_encode(char *dst, const char *src,
char *dstend, const char *reserved,
const char *term, int encode);
#ifdef HAVE_DNSSD
static void DNSSD_API http_resolve_cb(DNSServiceRef sdRef,
DNSServiceFlags flags,
uint32_t interfaceIndex,
DNSServiceErrorType errorCode,
const char *fullName,
const char *hostTarget,
uint16_t port, uint16_t txtLen,
const unsigned char *txtRecord,
void *context);
#endif /* HAVE_DNSSD */
#ifdef HAVE_AVAHI
static void http_client_cb(AvahiClient *client,
AvahiClientState state, void *simple_poll);
static int http_poll_cb(struct pollfd *pollfds, unsigned int num_pollfds,
int timeout, void *context);
static void http_resolve_cb(AvahiServiceResolver *resolver,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiResolverEvent event,
const char *name, const char *type,
const char *domain, const char *host_name,
const AvahiAddress *address, uint16_t port,
AvahiStringList *txt,
AvahiLookupResultFlags flags, void *context);
#endif /* HAVE_AVAHI */
/*
* 'httpAssembleURI()' - Assemble a uniform resource identifier from its
* components.
*
* This function escapes reserved characters in the URI depending on the
* value of the "encoding" argument. You should use this function in
* place of traditional string functions whenever you need to create a
* URI string.
*
* @since CUPS 1.2/macOS 10.5@
*/
http_uri_status_t /* O - URI status */
httpAssembleURI(
http_uri_coding_t encoding, /* I - Encoding flags */
char *uri, /* I - URI buffer */
int urilen, /* I - Size of URI buffer */
const char *scheme, /* I - Scheme name */
const char *username, /* I - Username */
const char *host, /* I - Hostname or address */
int port, /* I - Port number */
const char *resource) /* I - Resource */
{
char *ptr, /* Pointer into URI buffer */
*end; /* End of URI buffer */
/*
* Range check input...
*/
if (!uri || urilen < 1 || !scheme || port < 0)
{
if (uri)
*uri = '\0';
return (HTTP_URI_STATUS_BAD_ARGUMENTS);
}
/*
* Assemble the URI starting with the scheme...
*/
end = uri + urilen - 1;
ptr = http_copy_encode(uri, scheme, end, NULL, NULL, 0);
if (!ptr)
goto assemble_overflow;
if (!strcmp(scheme, "geo") || !strcmp(scheme, "mailto") || !strcmp(scheme, "tel"))
{
/*
* geo:, mailto:, and tel: only have :, no //...
*/
if (ptr < end)
*ptr++ = ':';
else
goto assemble_overflow;
}
else
{
/*
* Schemes other than geo:, mailto:, and tel: typically have //...
*/
if ((ptr + 2) < end)
{
*ptr++ = ':';
*ptr++ = '/';
*ptr++ = '/';
}
else
goto assemble_overflow;
}
/*
* Next the username and hostname, if any...
*/
if (host)
{
const char *hostptr; /* Pointer into hostname */
int have_ipv6; /* Do we have an IPv6 address? */
if (username && *username)
{
/*
* Add username@ first...
*/
ptr = http_copy_encode(ptr, username, end, "/?#[]@", NULL,
encoding & HTTP_URI_CODING_USERNAME);
if (!ptr)
goto assemble_overflow;
if (ptr < end)
*ptr++ = '@';
else
goto assemble_overflow;
}
/*
* Then add the hostname. Since IPv6 is a particular pain to deal
* with, we have several special cases to deal with. If we get
* an IPv6 address with brackets around it, assume it is already in
* URI format. Since DNS-SD service names can sometimes look like
* raw IPv6 addresses, we specifically look for "._tcp" in the name,
* too...
*/
for (hostptr = host,
have_ipv6 = strchr(host, ':') && !strstr(host, "._tcp");
*hostptr && have_ipv6;
hostptr ++)
if (*hostptr != ':' && !isxdigit(*hostptr & 255))
{
have_ipv6 = *hostptr == '%';
break;
}
if (have_ipv6)
{
/*
* We have a raw IPv6 address...
*/
if (strchr(host, '%') && !(encoding & HTTP_URI_CODING_RFC6874))
{
/*
* We have a link-local address, add "[v1." prefix...
*/
if ((ptr + 4) < end)
{
*ptr++ = '[';
*ptr++ = 'v';
*ptr++ = '1';
*ptr++ = '.';
}
else
goto assemble_overflow;
}
else
{
/*
* We have a normal (or RFC 6874 link-local) address, add "[" prefix...
*/
if (ptr < end)
*ptr++ = '[';
else
goto assemble_overflow;
}
/*
* Copy the rest of the IPv6 address, and terminate with "]".
*/
while (ptr < end && *host)
{
if (*host == '%')
{
/*
* Convert/encode zone separator
*/
if (encoding & HTTP_URI_CODING_RFC6874)
{
if (ptr >= (end - 2))
goto assemble_overflow;
*ptr++ = '%';
*ptr++ = '2';
*ptr++ = '5';
}
else
*ptr++ = '+';
host ++;
}
else
*ptr++ = *host++;
}
if (*host)
goto assemble_overflow;
if (ptr < end)
*ptr++ = ']';
else
goto assemble_overflow;
}
else
{
/*
* Otherwise, just copy the host string (the extra chars are not in the
* "reg-name" ABNF rule; anything <= SP or >= DEL plus % gets automatically
* percent-encoded.
*/
ptr = http_copy_encode(ptr, host, end, "\"#/:<>?@[\\]^`{|}", NULL,
encoding & HTTP_URI_CODING_HOSTNAME);
if (!ptr)
goto assemble_overflow;
}
/*
* Finish things off with the port number...
*/
if (port > 0)
{
snprintf(ptr, (size_t)(end - ptr + 1), ":%d", port);
ptr += strlen(ptr);
if (ptr >= end)
goto assemble_overflow;
}
}
/*
* Last but not least, add the resource string...
*/
if (resource)
{
char *query; /* Pointer to query string */
/*
* Copy the resource string up to the query string if present...
*/
query = strchr(resource, '?');
ptr = http_copy_encode(ptr, resource, end, NULL, "?",
encoding & HTTP_URI_CODING_RESOURCE);
if (!ptr)
goto assemble_overflow;
if (query)
{
/*
* Copy query string without encoding...
*/
ptr = http_copy_encode(ptr, query, end, NULL, NULL,
encoding & HTTP_URI_CODING_QUERY);
if (!ptr)
goto assemble_overflow;
}
}
else if (ptr < end)
*ptr++ = '/';
else
goto assemble_overflow;
/*
* Nul-terminate the URI buffer and return with no errors...
*/
*ptr = '\0';
return (HTTP_URI_STATUS_OK);
/*
* Clear the URI string and return an overflow error; I don't usually
* like goto's, but in this case it makes sense...
*/
assemble_overflow:
*uri = '\0';
return (HTTP_URI_STATUS_OVERFLOW);
}
/*
* 'httpAssembleURIf()' - Assemble a uniform resource identifier from its
* components with a formatted resource.
*
* This function creates a formatted version of the resource string
* argument "resourcef" and escapes reserved characters in the URI
* depending on the value of the "encoding" argument. You should use
* this function in place of traditional string functions whenever
* you need to create a URI string.
*
* @since CUPS 1.2/macOS 10.5@
*/
http_uri_status_t /* O - URI status */
httpAssembleURIf(
http_uri_coding_t encoding, /* I - Encoding flags */
char *uri, /* I - URI buffer */
int urilen, /* I - Size of URI buffer */
const char *scheme, /* I - Scheme name */
const char *username, /* I - Username */
const char *host, /* I - Hostname or address */
int port, /* I - Port number */
const char *resourcef, /* I - Printf-style resource */
...) /* I - Additional arguments as needed */
{
va_list ap; /* Pointer to additional arguments */
char resource[1024]; /* Formatted resource string */
int bytes; /* Bytes in formatted string */
/*
* Range check input...
*/
if (!uri || urilen < 1 || !scheme || port < 0 || !resourcef)
{
if (uri)
*uri = '\0';
return (HTTP_URI_STATUS_BAD_ARGUMENTS);
}
/*
* Format the resource string and assemble the URI...
*/
va_start(ap, resourcef);
bytes = vsnprintf(resource, sizeof(resource), resourcef, ap);
va_end(ap);
if ((size_t)bytes >= sizeof(resource))
{
*uri = '\0';
return (HTTP_URI_STATUS_OVERFLOW);
}
else
return (httpAssembleURI(encoding, uri, urilen, scheme, username, host,
port, resource));
}
/*
* 'httpAssembleUUID()' - Assemble a name-based UUID URN conforming to RFC 4122.
*
* This function creates a unique 128-bit identifying number using the server
* name, port number, random data, and optionally an object name and/or object
* number. The result is formatted as a UUID URN as defined in RFC 4122.
*
* The buffer needs to be at least 46 bytes in size.
*
* @since CUPS 1.7/macOS 10.9@
*/
char * /* I - UUID string */
httpAssembleUUID(const char *server, /* I - Server name */
int port, /* I - Port number */
const char *name, /* I - Object name or NULL */
int number, /* I - Object number or 0 */
char *buffer, /* I - String buffer */
size_t bufsize) /* I - Size of buffer */
{
char data[1024]; /* Source string for MD5 */
unsigned char md5sum[16]; /* MD5 digest/sum */
/*
* Build a version 3 UUID conforming to RFC 4122.
*
* Start with the MD5 sum of the server, port, object name and
* number, and some random data on the end.
*/
snprintf(data, sizeof(data), "%s:%d:%s:%d:%04x:%04x", server,
port, name ? name : server, number,
(unsigned)CUPS_RAND() & 0xffff, (unsigned)CUPS_RAND() & 0xffff);
cupsHashData("md5", (unsigned char *)data, strlen(data), md5sum, sizeof(md5sum));
/*
* Generate the UUID from the MD5...
*/
snprintf(buffer, bufsize,
"urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
"%02x%02x%02x%02x%02x%02x",
md5sum[0], md5sum[1], md5sum[2], md5sum[3], md5sum[4], md5sum[5],
(md5sum[6] & 15) | 0x30, md5sum[7], (md5sum[8] & 0x3f) | 0x40,
md5sum[9], md5sum[10], md5sum[11], md5sum[12], md5sum[13],
md5sum[14], md5sum[15]);
return (buffer);
}
/*
* 'httpDecode64()' - Base64-decode a string.
*
* This function is deprecated. Use the httpDecode64_2() function instead
* which provides buffer length arguments.
*
* @deprecated@ @exclude all@
*/
char * /* O - Decoded string */
httpDecode64(char *out, /* I - String to write to */
const char *in) /* I - String to read from */
{
int outlen; /* Output buffer length */
/*
* Use the old maximum buffer size for binary compatibility...
*/
outlen = 512;
return (httpDecode64_2(out, &outlen, in));
}
/*
* 'httpDecode64_2()' - Base64-decode a string.
*
* The caller must initialize "outlen" to the maximum size of the decoded
* string before calling @code httpDecode64_2@. On return "outlen" contains the
* decoded length of the string.
*
* @since CUPS 1.1.21/macOS 10.4@
*/
char * /* O - Decoded string */
httpDecode64_2(char *out, /* I - String to write to */
int *outlen, /* IO - Size of output string */
const char *in) /* I - String to read from */
{
int pos; /* Bit position */
unsigned base64; /* Value of this character */
char *outptr, /* Output pointer */
*outend; /* End of output buffer */
/*
* Range check input...
*/
if (!out || !outlen || *outlen < 1 || !in)
return (NULL);
if (!*in)
{
*out = '\0';
*outlen = 0;
return (out);
}
/*
* Convert from base-64 to bytes...
*/
for (outptr = out, outend = out + *outlen - 1, pos = 0; *in != '\0'; in ++)
{
/*
* Decode this character into a number from 0 to 63...
*/
if (*in >= 'A' && *in <= 'Z')
base64 = (unsigned)(*in - 'A');
else if (*in >= 'a' && *in <= 'z')
base64 = (unsigned)(*in - 'a' + 26);
else if (*in >= '0' && *in <= '9')
base64 = (unsigned)(*in - '0' + 52);
else if (*in == '+')
base64 = 62;
else if (*in == '/')
base64 = 63;
else if (*in == '=')
break;
else
continue;
/*
* Store the result in the appropriate chars...
*/
switch (pos)
{
case 0 :
if (outptr < outend)
*outptr = (char)(base64 << 2);
pos ++;
break;
case 1 :
if (outptr < outend)
*outptr++ |= (char)((base64 >> 4) & 3);
if (outptr < outend)
*outptr = (char)((base64 << 4) & 255);
pos ++;
break;
case 2 :
if (outptr < outend)
*outptr++ |= (char)((base64 >> 2) & 15);
if (outptr < outend)
*outptr = (char)((base64 << 6) & 255);
pos ++;
break;
case 3 :
if (outptr < outend)
*outptr++ |= (char)base64;
pos = 0;
break;
}
}
*outptr = '\0';
/*
* Return the decoded string and size...
*/
*outlen = (int)(outptr - out);
return (out);
}
/*
* 'httpEncode64()' - Base64-encode a string.
*
* This function is deprecated. Use the httpEncode64_2() function instead
* which provides buffer length arguments.
*
* @deprecated@ @exclude all@
*/
char * /* O - Encoded string */
httpEncode64(char *out, /* I - String to write to */
const char *in) /* I - String to read from */
{
return (httpEncode64_2(out, 512, in, (int)strlen(in)));
}
/*
* 'httpEncode64_2()' - Base64-encode a string.
*
* @since CUPS 1.1.21/macOS 10.4@
*/
char * /* O - Encoded string */
httpEncode64_2(char *out, /* I - String to write to */
int outlen, /* I - Maximum size of output string */
const char *in, /* I - String to read from */
int inlen) /* I - Size of input string */
{
char *outptr, /* Output pointer */
*outend; /* End of output buffer */
static const char base64[] = /* Base64 characters... */
{
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789"
"+/"
};
/*
* Range check input...
*/
if (!out || outlen < 1 || !in)
return (NULL);
/*
* Convert bytes to base-64...
*/
for (outptr = out, outend = out + outlen - 1; inlen > 0; in ++, inlen --)
{
/*
* Encode the up to 3 characters as 4 Base64 numbers...
*/
if (outptr < outend)
*outptr ++ = base64[(in[0] & 255) >> 2];
if (outptr < outend)
{
if (inlen > 1)
*outptr ++ = base64[(((in[0] & 255) << 4) | ((in[1] & 255) >> 4)) & 63];
else
*outptr ++ = base64[((in[0] & 255) << 4) & 63];
}
in ++;
inlen --;
if (inlen <= 0)
{
if (outptr < outend)
*outptr ++ = '=';
if (outptr < outend)
*outptr ++ = '=';
break;
}
if (outptr < outend)
{
if (inlen > 1)
*outptr ++ = base64[(((in[0] & 255) << 2) | ((in[1] & 255) >> 6)) & 63];
else
*outptr ++ = base64[((in[0] & 255) << 2) & 63];
}
in ++;
inlen --;
if (inlen <= 0)
{
if (outptr < outend)
*outptr ++ = '=';
break;
}
if (outptr < outend)
*outptr ++ = base64[in[0] & 63];
}
*outptr = '\0';
/*
* Return the encoded string...
*/
return (out);
}
/*
* 'httpGetDateString()' - Get a formatted date/time string from a time value.
*
* @deprecated@ @exclude all@
*/
const char * /* O - Date/time string */
httpGetDateString(time_t t) /* I - Time in seconds */
{
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
return (httpGetDateString2(t, cg->http_date, sizeof(cg->http_date)));
}
/*
* 'httpGetDateString2()' - Get a formatted date/time string from a time value.
*
* @since CUPS 1.2/macOS 10.5@
*/
const char * /* O - Date/time string */
httpGetDateString2(time_t t, /* I - Time in seconds */
char *s, /* I - String buffer */
int slen) /* I - Size of string buffer */
{
struct tm tdate; /* UNIX date/time data */
gmtime_r(&t, &tdate);
snprintf(s, (size_t)slen, "%s, %02d %s %d %02d:%02d:%02d GMT", http_days[tdate.tm_wday], tdate.tm_mday, http_months[tdate.tm_mon], tdate.tm_year + 1900, tdate.tm_hour, tdate.tm_min, tdate.tm_sec);
return (s);
}
/*
* 'httpGetDateTime()' - Get a time value from a formatted date/time string.
*/
time_t /* O - Time in seconds */
httpGetDateTime(const char *s) /* I - Date/time string */
{
int i; /* Looping var */
char mon[16]; /* Abbreviated month name */
int day, year; /* Day of month and year */
int hour, min, sec; /* Time */
int days; /* Number of days since 1970 */
static const int normal_days[] = /* Days to a month, normal years */
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
static const int leap_days[] = /* Days to a month, leap years */
{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
DEBUG_printf(("2httpGetDateTime(s=\"%s\")", s));
/*
* Extract the date and time from the formatted string...
*/
if (sscanf(s, "%*s%d%15s%d%d:%d:%d", &day, mon, &year, &hour, &min, &sec) < 6)
return (0);
DEBUG_printf(("4httpGetDateTime: day=%d, mon=\"%s\", year=%d, hour=%d, "
"min=%d, sec=%d", day, mon, year, hour, min, sec));
/*
* Check for invalid year (RFC 7231 says it's 4DIGIT)
*/
if (year > 9999)
return (0);
/*
* Convert the month name to a number from 0 to 11.
*/
for (i = 0; i < 12; i ++)
if (!_cups_strcasecmp(mon, http_months[i]))
break;
if (i >= 12)
return (0);
DEBUG_printf(("4httpGetDateTime: i=%d", i));
/*
* Now convert the date and time to a UNIX time value in seconds since
* 1970. We can't use mktime() since the timezone may not be UTC but
* the date/time string *is* UTC.
*/
if ((year & 3) == 0 && ((year % 100) != 0 || (year % 400) == 0))
days = leap_days[i] + day - 1;
else
days = normal_days[i] + day - 1;
DEBUG_printf(("4httpGetDateTime: days=%d", days));
days += (year - 1970) * 365 + /* 365 days per year (normally) */
((year - 1) / 4 - 492) - /* + leap days */
((year - 1) / 100 - 19) + /* - 100 year days */
((year - 1) / 400 - 4); /* + 400 year days */
DEBUG_printf(("4httpGetDateTime: days=%d\n", days));
return (days * 86400 + hour * 3600 + min * 60 + sec);
}
/*
* 'httpSeparate()' - Separate a Universal Resource Identifier into its
* components.
*
* This function is deprecated; use the httpSeparateURI() function instead.
*
* @deprecated@ @exclude all@
*/
void
httpSeparate(const char *uri, /* I - Universal Resource Identifier */
char *scheme, /* O - Scheme [32] (http, https, etc.) */
char *username, /* O - Username [1024] */
char *host, /* O - Hostname [1024] */
int *port, /* O - Port number to use */
char *resource) /* O - Resource/filename [1024] */
{
httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, 32, username,
HTTP_MAX_URI, host, HTTP_MAX_URI, port, resource,
HTTP_MAX_URI);
}
/*
* 'httpSeparate2()' - Separate a Universal Resource Identifier into its
* components.
*
* This function is deprecated; use the httpSeparateURI() function instead.
*
* @since CUPS 1.1.21/macOS 10.4@
* @deprecated@ @exclude all@
*/
void
httpSeparate2(const char *uri, /* I - Universal Resource Identifier */
char *scheme, /* O - Scheme (http, https, etc.) */
int schemelen, /* I - Size of scheme buffer */
char *username, /* O - Username */
int usernamelen, /* I - Size of username buffer */
char *host, /* O - Hostname */
int hostlen, /* I - Size of hostname buffer */
int *port, /* O - Port number to use */
char *resource, /* O - Resource/filename */
int resourcelen) /* I - Size of resource buffer */
{
httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, schemelen, username,
usernamelen, host, hostlen, port, resource, resourcelen);
}
/*
* 'httpSeparateURI()' - Separate a Universal Resource Identifier into its
* components.
*
* @since CUPS 1.2/macOS 10.5@
*/
http_uri_status_t /* O - Result of separation */
httpSeparateURI(
http_uri_coding_t decoding, /* I - Decoding flags */
const char *uri, /* I - Universal Resource Identifier */
char *scheme, /* O - Scheme (http, https, etc.) */
int schemelen, /* I - Size of scheme buffer */
char *username, /* O - Username */
int usernamelen, /* I - Size of username buffer */
char *host, /* O - Hostname */
int hostlen, /* I - Size of hostname buffer */
int *port, /* O - Port number to use */
char *resource, /* O - Resource/filename */
int resourcelen) /* I - Size of resource buffer */
{
char *ptr, /* Pointer into string... */
*end; /* End of string */
const char *sep; /* Separator character */
http_uri_status_t status; /* Result of separation */
/*
* Initialize everything to blank...
*/
if (scheme && schemelen > 0)
*scheme = '\0';
if (username && usernamelen > 0)
*username = '\0';
if (host && hostlen > 0)
*host = '\0';
if (port)
*port = 0;
if (resource && resourcelen > 0)
*resource = '\0';
/*
* Range check input...
*/
if (!uri || !port || !scheme || schemelen <= 0 || !username ||
usernamelen <= 0 || !host || hostlen <= 0 || !resource ||
resourcelen <= 0)
return (HTTP_URI_STATUS_BAD_ARGUMENTS);
if (!*uri)
return (HTTP_URI_STATUS_BAD_URI);
/*
* Grab the scheme portion of the URI...
*/
status = HTTP_URI_STATUS_OK;