-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathdhttp.c
869 lines (776 loc) · 25 KB
/
dhttp.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
/**
* @file
*
* Read a range of data from a remote dataset.
*
* Copyright 2018 University Corporation for Atmospheric
* Research/Unidata. See COPYRIGHT file for more info.
*/
#include "config.h"
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#define CURL_DISABLE_TYPECHECK 1
#include <curl/curl.h>
#include "netcdf.h"
#include "nclog.h"
#include "ncbytes.h"
#include "nclist.h"
#include "ncuri.h"
#include "ncauth.h"
#ifdef NETCDF_ENABLE_S3
#include "ncs3sdk.h"
#endif
#include "nchttp.h"
#undef TRACE
#define CURLERR(e) reporterror(state,(e))
#if 0
static const char* LENGTH_ACCEPT[] = {"content-length","accept-ranges",NULL};
#endif
static const char* CONTENTLENGTH[] = {"content-length",NULL};
/* Forward */
static int nc_http_set_method(NC_HTTP_STATE* state, HTTPMETHOD method);
static int nc_http_set_response(NC_HTTP_STATE* state, NCbytes* buf);
static int nc_http_set_payload(NC_HTTP_STATE* state, size_t len, void* payload);
static int setupconn(NC_HTTP_STATE* state, const char* objecturl);
static int execute(NC_HTTP_STATE* state);
static int headerson(NC_HTTP_STATE* state, const char** which);
static void headersoff(NC_HTTP_STATE* state);
static void showerrors(NC_HTTP_STATE* state);
static int reporterror(NC_HTTP_STATE* state, CURLcode cstat);
static int lookupheader(NC_HTTP_STATE* state, const char* key, const char** valuep);
static int my_trace(CURL *handle, curl_infotype type, char *data, size_t size,void *userp);
#ifdef TRACE
static void
dbgflush() {
fflush(stderr);
fflush(stdout);
}
static void
Trace(const char* fcn)
{
fprintf(stdout,"xxx: %s\n",fcn);
dbgflush();
}
#else
#define dbgflush()
#define Trace(fcn)
#endif /*TRACE*/
/**************************************************/
/**
@param statep return a pointer to an allocated NC_HTTP_STATE
*/
int
nc_http_open(const char* url, NC_HTTP_STATE** statep)
{
return nc_http_open_verbose(url,0,statep);
}
int
nc_http_open_verbose(const char* path, int verbose, NC_HTTP_STATE** statep)
{
int stat = NC_NOERR;
NC_HTTP_STATE* state = NULL;
NCURI* uri = NULL;
Trace("open");
ncuriparse(path,&uri);
if(uri == NULL) {stat = NCTHROW(NC_EURL); goto done;}
if((state = calloc(1,sizeof(NC_HTTP_STATE))) == NULL)
{stat = NCTHROW(NC_ENOMEM); goto done;}
state->path = strdup(path);
state->url = uri; uri = NULL;
#ifdef NETCDF_ENABLE_S3
state->format = (NC_iss3(state->url,NULL)?HTTPS3:HTTPCURL);
#else
state->format = HTTPCURL;
#endif
switch (state->format) {
case HTTPCURL: {
/* initialize curl*/
state->curl.curl = curl_easy_init();
if (state->curl.curl == NULL) {stat = NCTHROW(NC_ECURL); goto done;}
showerrors(state);
state->errmsg = state->curl.errbuf;
if(verbose) {
long onoff = 1;
CURLcode cstat = CURLE_OK;
cstat = CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_VERBOSE, onoff));
if(cstat != CURLE_OK) {stat = NCTHROW(NC_ECURL); goto done;}
cstat = CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_DEBUGFUNCTION, my_trace));
if(cstat != CURLE_OK) {stat = NCTHROW(NC_ECURL); goto done;}
}
} break;
#ifdef NETCDF_ENABLE_S3
case HTTPS3: {
if((state->s3.info = (NCS3INFO*)calloc(1,sizeof(NCS3INFO)))==NULL)
{stat = NCTHROW(NC_ENOMEM); goto done;}
if((stat = NC_s3urlprocess(state->url,state->s3.info,NULL))) goto done;
if((state->s3.s3client = NC_s3sdkcreateclient(state->s3.info))==NULL)
{stat = NCTHROW(NC_EURL); goto done;}
} break;
#endif
default: return NCTHROW(NC_ENOTBUILT);
}
stat = nc_http_reset(state);
if(statep) {*statep = state; state = NULL;}
done:
if(state) nc_http_close(state);
dbgflush();
return NCTHROW(stat);
}
int
nc_http_close(NC_HTTP_STATE* state)
{
int stat = NC_NOERR;
Trace("close");
if(state == NULL) return NCTHROW(stat);
switch (state->format) {
case HTTPCURL:
if(state->curl.curl != NULL)
(void)curl_easy_cleanup(state->curl.curl);
nclistfreeall(state->curl.response.headset); state->curl.response.headset = NULL;
nclistfreeall(state->curl.response.headers); state->curl.response.headers = NULL;
ncbytesfree(state->curl.response.buf);
nclistfreeall(state->curl.request.headers); state->curl.request.headers = NULL;
break;
#ifdef NETCDF_ENABLE_S3
case HTTPS3: {
if(state->s3.s3client)
NC_s3sdkclose(state->s3.s3client, state->s3.info, 0, NULL);
NC_s3clear(state->s3.info);
nullfree(state->s3.info);
state->s3.s3client = NULL;
} break;
#endif
default: stat = NCTHROW(NC_ENOTBUILT); goto done;
}
nullfree(state->path);
ncurifree(state->url);
nullfree(state);
done:
dbgflush();
return NCTHROW(stat);
}
/* Reset after a request */
int
nc_http_reset(NC_HTTP_STATE* state)
{
int stat = NC_NOERR;
CURLcode cstat = CURLE_OK;
switch (state->format) {
case HTTPCURL:
cstat = CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_HTTPGET, 1L));
if(cstat != CURLE_OK) {stat = NCTHROW(NC_ECURL); goto done;}
cstat = CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_NOBODY, 0L));
if(cstat != CURLE_OK) {stat = NCTHROW(NC_ECURL); goto done;}
cstat = CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_UPLOAD, 0L));
if(cstat != CURLE_OK) {stat = NCTHROW(NC_ECURL); goto done;}
cstat = curl_easy_setopt(state->curl.curl, CURLOPT_CUSTOMREQUEST, NULL);
if(cstat != CURLE_OK) {stat = NCTHROW(NC_ECURL); goto done;}
cstat = curl_easy_setopt(state->curl.curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)-1);
if(cstat != CURLE_OK) {stat = NCTHROW(NC_ECURL); goto done;}
state->curl.request.method = HTTPGET;
(void)CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_WRITEFUNCTION, NULL));
(void)CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_WRITEDATA, NULL));
(void)CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_READFUNCTION, NULL));
(void)CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_READDATA, NULL));
headersoff(state);
break;
#ifdef NETCDF_ENABLE_S3
case HTTPS3:
break; /* Done automatically */
#endif
default: stat = NCTHROW(NC_ENOTBUILT); goto done;
}
done:
return NCTHROW(stat);
}
/**************************************************/
/**************************************************/
/**
@param state state handle
@param objecturl to read
@param start starting offset
@param count number of bytes to read
@param buf store read data here -- caller must allocate and free
*/
int
nc_http_read(NC_HTTP_STATE* state, size64_t start, size64_t count, NCbytes* buf)
{
int stat = NC_NOERR;
char range[64];
CURLcode cstat = CURLE_OK;
Trace("read");
if(count == 0)
goto done; /* do not attempt to read */
switch (state->format) {
case HTTPCURL:
if((stat = nc_http_set_response(state,buf))) goto fail;
if((stat = setupconn(state,state->path)))
goto fail;
/* Set to read byte range */
snprintf(range,sizeof(range),"%ld-%ld",(long)start,(long)((start+count)-1));
cstat = CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_RANGE, range));
if(cstat != CURLE_OK)
{stat = NCTHROW(NC_ECURL); goto done;}
if((stat = execute(state)))
goto done;
break;
#ifdef NETCDF_ENABLE_S3
case HTTPS3: {
/* Make sure buf has enough space allocated */
ncbytessetalloc(buf,count);
ncbytessetlength(buf,count);
if((stat = NC_s3sdkread(state->s3.s3client,
state->s3.info->bucket,
state->s3.info->rootkey,
start,
count,
ncbytescontents(buf),
&state->errmsg))) goto done;
} break;
#endif
default: stat = NCTHROW(NC_ENOTBUILT); goto done;
}
done:
nc_http_reset(state);
if(state->format == HTTPCURL)
state->curl.response.buf = NULL;
dbgflush();
return NCTHROW(stat);
fail:
stat = NCTHROW(NC_ECURL);
goto done;
}
/**
@param state state handle
@param objectpath to write
@param payload send as body of a PUT
*/
int
nc_http_write(NC_HTTP_STATE* state, NCbytes* payload)
{
int stat = NC_NOERR;
Trace("write");
if(payload == NULL || ncbyteslength(payload) == 0) goto done;
switch (state->format) {
case HTTPCURL:
if((stat = nc_http_set_payload(state,ncbyteslength(payload),ncbytescontents(payload)))) goto fail;
if((stat = nc_http_set_method(state,HTTPPUT))) goto fail;
if((stat = setupconn(state,state->path))) goto fail;
if((stat = execute(state)))
goto done;
break;
#ifdef NETCDF_ENABLE_S3
case HTTPS3:
if((stat = NC_s3sdkwriteobject(state->s3.s3client,
state->s3.info->bucket,
state->s3.info->rootkey,
ncbyteslength(payload),
ncbytescontents(payload),
&state->errmsg))) goto done;
break;
#endif
default: stat = NCTHROW(NC_ENOTBUILT); goto done;
}
done:
nc_http_reset(state);
return NCTHROW(stat);
fail:
stat = NCTHROW(NC_ECURL);
goto done;
}
/**
Return length of an object.
Assume URL etc has already been set.
@param curl curl handle
*/
int
nc_http_size(NC_HTTP_STATE* state, long long* sizep)
{
int stat = NC_NOERR;
const char* hdr = NULL;
Trace("size");
if(sizep == NULL)
goto done; /* do not attempt to read */
switch (state->format) {
case HTTPCURL:
if((stat = nc_http_set_method(state,HTTPHEAD))) goto done;
if((stat = setupconn(state,state->path)))
goto done;
/* Make sure we get headers */
if((stat = headerson(state,CONTENTLENGTH))) goto done;
state->httpcode = 200;
if((stat = execute(state)))
goto done;
if(nclistlength(state->curl.response.headers) == 0)
{stat = NCTHROW(NC_EURL); goto done;}
/* Get the content length header */
if((stat = lookupheader(state,"content-length",&hdr))==NC_NOERR)
sscanf(hdr,"%llu",sizep);
break;
#ifdef NETCDF_ENABLE_S3
case HTTPS3: {
size64_t len = 0;
if((stat = NC_s3sdkinfo(state->s3.s3client,state->s3.info->bucket,state->s3.info->rootkey,&len,&state->errmsg))) goto done;
if(sizep) *sizep = len;
} break;
#endif
default: stat = NCTHROW(NC_ENOTBUILT); goto done;
}
done:
nc_http_reset(state);
if(state->format == HTTPCURL)
headersoff(state);
dbgflush();
return NCTHROW(stat);
}
/**************************************************/
/* Set misc parameters */
static int
nc_http_set_method(NC_HTTP_STATE* state, HTTPMETHOD method)
{
int stat = NC_NOERR;
CURLcode cstat = CURLE_OK;
switch (method) {
case HTTPGET:
cstat = CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_HTTPGET, 1L));
break;
case HTTPHEAD:
cstat = CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_HTTPGET, 1L));
cstat = CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_NOBODY, 1L));
break;
case HTTPPUT:
cstat = CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_UPLOAD, 1L));
break;
case HTTPDELETE:
cstat = curl_easy_setopt(state->curl.curl, CURLOPT_CUSTOMREQUEST, "DELETE");
cstat = CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_NOBODY, 1L));
break;
default: stat = NCTHROW(NC_EINVAL); break;
}
if(cstat != CURLE_OK) {stat = NCTHROW(NC_ECURL); goto done;}
state->curl.request.method = method;
done:
return NCTHROW(stat);
}
static int
nc_http_set_payload(NC_HTTP_STATE* state, size_t size, void* payload)
{
int stat = NC_NOERR;
state->curl.request.payloadsize = size;
state->curl.request.payload = payload;
state->curl.request.payloadpos = 0;
return NCTHROW(stat);
}
static int
nc_http_set_response(NC_HTTP_STATE* state, NCbytes* buf)
{
int stat = NC_NOERR;
state->curl.response.buf = buf;
return NCTHROW(stat);
}
#if 0
static int
nc_http_response_headset(NC_HTTP_STATE* state, const NClist* keys)
{
int i;
if(keys == NULL) return NC_NOERR;
if(state->curl.response.headset == NULL)
state->curl.response.headset = nclistnew();
for(i=0;i<nclistlength(keys);i++) {
const char* key = (const char*)nclistget(keys,i);
if(!nclistmatch(state->curl.response.headset,key,0)) /* remove duplicates */
nclistpush(state->curl.response.headset,strdup(key));
}
return NC_NOERR;
}
static int
nc_http_response_headers(NC_HTTP_STATE* state, NClist** headersp)
{
NClist* headers = NULL;
if(headersp != NULL) {
headers = nclistclone(state->curl.response.headers,1);
*headersp = headers; headers = NULL;
}
return NC_NOERR;
}
static int
nc_http_request_setheaders(NC_HTTP_STATE* state, const NClist* headers)
{
nclistfreeall(state->curl.request.headers);
state->curl.request.headers = nclistclone(headers,1);
return NC_NOERR;
}
#endif
/**************************************************/
static size_t
ReadMemoryCallback(char* buffer, size_t size, size_t nmemb, void *data)
{
NC_HTTP_STATE* state = data;
size_t transfersize = size * nmemb;
size_t avail = (state->curl.request.payloadsize - state->curl.request.payloadpos);
Trace("ReadMemoryCallback");
if(transfersize == 0)
nclog(NCLOGWARN,"ReadMemoryCallback: zero sized buffer");
if(transfersize > avail) transfersize = avail;
memcpy(buffer,((char*)state->curl.request.payload)+state->curl.request.payloadpos,transfersize);
state->curl.request.payloadpos += transfersize;
return transfersize;
}
static size_t
WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
NC_HTTP_STATE* state = data;
size_t realsize = size * nmemb;
Trace("WriteMemoryCallback");
if(realsize == 0)
nclog(NCLOGWARN,"WriteMemoryCallback: zero sized chunk");
ncbytesappendn(state->curl.response.buf, ptr, realsize);
return realsize;
}
static void
trim(char* s)
{
size_t l = strlen(s);
char* p = s;
char* q = s + l;
if(l == 0) return;
q--; /* point to last char of string */
/* Walk backward to first non-whitespace */
for(;q > p;q--) {
if(*q > ' ') break; /* found last non-whitespace */
}
/* invariant: p == q || *q > ' ' */
if(p == q) /* string is all whitespace */
{*p = '\0';}
else {/* *q is last non-whitespace */
q++; /* point to actual whitespace */
*q = '\0';
}
/* Ok, skip past leading whitespace */
for(p=s;*p;p++) {if(*p > ' ') break;}
/* invariant: *p == '\0' || *p > ' ' */
if(*p == 0) return; /* no leading whitespace */
/* Ok, overwrite any leading whitespace */
for(q=s;*p;) {*q++ = *p++;}
*q = '\0';
}
static size_t
HeaderCallback(char *buffer, size_t size, size_t nitems, void *data)
{
size_t realsize = size * nitems;
char* name = NULL;
char* value = NULL;
char* p = NULL;
size_t i;
int havecolon;
NC_HTTP_STATE* state = data;
int match;
const char* hdr;
Trace("HeaderCallback");
if(realsize == 0)
nclog(NCLOGWARN,"HeaderCallback: zero sized chunk");
i = 0;
/* Look for colon separator */
for(p=buffer;(i < realsize) && (*p != ':');p++,i++);
havecolon = (i < realsize);
if(i == 0)
nclog(NCLOGWARN,"HeaderCallback: malformed header: %s",buffer);
name = malloc(i+1);
memcpy(name,buffer,i);
name[i] = '\0';
if(state->curl.response.headset != NULL) {
for(match=0,i=0;i<nclistlength(state->curl.response.headset);i++) {
hdr = (const char*)nclistget(state->curl.response.headset,i);
if(strcasecmp(hdr,name)==0) {match = 1; break;}
}
if(!match) goto done;
}
/* Capture this header */
value = NULL;
if(havecolon) {
size_t vlen = (realsize - i);
value = malloc(vlen+1);
p++; /* skip colon */
memcpy(value,p,vlen);
value[vlen] = '\0';
trim(value);
}
if(state->curl.response.headers == NULL)
state->curl.response.headers = nclistnew();
nclistpush(state->curl.response.headers,name);
name = NULL;
if(value == NULL) value = strdup("");
nclistpush(state->curl.response.headers,value);
value = NULL;
done:
nullfree(name);
return realsize;
}
static int
setupconn(NC_HTTP_STATE* state, const char* objecturl)
{
int stat = NC_NOERR;
CURLcode cstat = CURLE_OK;
if(objecturl != NULL) {
/* Set the URL */
#ifdef TRACE
fprintf(stderr,"curl.setup: url |%s|\n",objecturl);
#endif
cstat = CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_URL, (void*)objecturl));
if (cstat != CURLE_OK) goto fail;
}
/* Set options */
cstat = CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_TIMEOUT, 100)); /* 30sec timeout*/
if (cstat != CURLE_OK) goto fail;
cstat = CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_CONNECTTIMEOUT, 100));
if (cstat != CURLE_OK) goto fail;
cstat = CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_NOPROGRESS, 1));
if (cstat != CURLE_OK) goto fail;
cstat = curl_easy_setopt(state->curl.curl, CURLOPT_FOLLOWLOCATION, 1);
if (cstat != CURLE_OK) goto fail;
/* Pull some values from .rc tables */
{
NCURI* uri = NULL;
char* hostport = NULL;
char* value = NULL;
ncuriparse(objecturl,&uri);
if(uri == NULL) goto fail;
hostport = NC_combinehostport(uri);
ncurifree(uri); uri = NULL;
value = NC_rclookup("HTTP.SSL.CAINFO",hostport,NULL);
nullfree(hostport); hostport = NULL;
if(value == NULL)
value = NC_rclookup("HTTP.SSL.CAINFO",NULL,NULL);
if(value != NULL) {
cstat = CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_CAINFO, value));
if (cstat != CURLE_OK) goto fail;
}
}
/* Set the method */
if((stat = nc_http_set_method(state,state->curl.request.method))) goto done;
if(state->curl.response.buf) {
/* send all data to this function */
cstat = CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback));
if (cstat != CURLE_OK) goto fail;
/* Set argument for WriteMemoryCallback */
cstat = CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_WRITEDATA, (void*)state));
if (cstat != CURLE_OK) goto fail;
} else {/* turn off data capture */
(void)CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_WRITEFUNCTION, NULL));
(void)CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_WRITEDATA, NULL));
}
if(state->curl.request.payloadsize > 0) {
state->curl.request.payloadpos = 0; /* track reading */
/* send all data to this function */
cstat = CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_READFUNCTION, ReadMemoryCallback));
if (cstat != CURLE_OK) goto fail;
/* Set argument for ReadMemoryCallback */
cstat = CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_READDATA, (void*)state));
if (cstat != CURLE_OK) goto fail;
} else {/* turn off data capture */
(void)CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_READFUNCTION, NULL));
(void)CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_READDATA, NULL));
}
/* Do method specific actions */
switch(state->curl.request.method) {
case HTTPPUT:
if(state->curl.request.payloadsize > 0)
cstat = curl_easy_setopt(state->curl.curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)state->curl.request.payloadsize);
break;
default: break;
}
done:
return NCTHROW(stat);
fail:
/* Turn off header capture */
headersoff(state);
stat = NCTHROW(NC_ECURL);
goto done;
}
static int
execute(NC_HTTP_STATE* state)
{
int stat = NC_NOERR;
CURLcode cstat = CURLE_OK;
cstat = CURLERR(curl_easy_perform(state->curl.curl));
if(cstat != CURLE_OK) goto fail;
cstat = CURLERR(curl_easy_getinfo(state->curl.curl,CURLINFO_RESPONSE_CODE,&state->httpcode));
if(cstat != CURLE_OK) state->httpcode = 0;
done:
return NCTHROW(stat);
fail:
stat = NCTHROW(NC_ECURL);
goto done;
}
static int
headerson(NC_HTTP_STATE* state, const char** headset)
{
int stat = NC_NOERR;
CURLcode cstat = CURLE_OK;
const char** p;
if(state->curl.response.headers != NULL)
nclistfreeall(state->curl.response.headers);
state->curl.response.headers = nclistnew();
if(state->curl.response.headset != NULL)
nclistfreeall(state->curl.response.headset);
state->curl.response.headset = nclistnew();
for(p=headset;*p;p++)
nclistpush(state->curl.response.headset,strdup(*p));
cstat = CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_HEADERFUNCTION, HeaderCallback));
if(cstat != CURLE_OK) goto fail;
cstat = CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_HEADERDATA, (void*)state));
if (cstat != CURLE_OK) goto fail;
done:
return NCTHROW(stat);
fail:
stat = NCTHROW(NC_ECURL);
goto done;
}
static void
headersoff(NC_HTTP_STATE* state)
{
nclistfreeall(state->curl.response.headers);
state->curl.response.headers = NULL;
(void)CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_HEADERFUNCTION, NULL));
(void)CURLERR(curl_easy_setopt(state->curl.curl, CURLOPT_HEADERDATA, NULL));
}
static int
lookupheader(NC_HTTP_STATE* state, const char* key, const char** valuep)
{
size_t i;
const char* value = NULL;
/* Get the content length header */
for(i=0;i<nclistlength(state->curl.response.headers);i+=2) {
char* s = nclistget(state->curl.response.headers,i);
if(strcasecmp(s,key)==0) {
value = nclistget(state->curl.response.headers,i+1);
break;
}
}
if(value == NULL) return NCTHROW(NC_ENOOBJECT);
if(valuep)
*valuep = value;
return NC_NOERR;
}
static void
showerrors(NC_HTTP_STATE* state)
{
(void)curl_easy_setopt(state->curl.curl, CURLOPT_ERRORBUFFER, state->curl.errbuf);
}
static int
reporterror(NC_HTTP_STATE* state, CURLcode cstat)
{
if(cstat != CURLE_OK)
fprintf(stderr,"curlcode: (%d)%s : %s\n",
cstat,curl_easy_strerror(cstat),
state->errmsg?state->errmsg:"?");
return cstat;
}
static
void dump(const char *text, FILE *stream, unsigned char *ptr, size_t size)
{
size_t i;
size_t c;
unsigned int width=0x10;
fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
text, (long)size, (long)size);
for(i=0; i<size; i+= width) {
fprintf(stream, "%4.4lx: ", (long)i);
/* show hex to the left */
for(c = 0; c < width; c++) {
if(i+c < size)
fprintf(stream, "%02x ", ptr[i+c]);
else
fputs(" ", stream);
}
/* show data on the right */
for(c = 0; (c < width) && (i+c < size); c++) {
const int x = (ptr[i+c] >= 0x20 && ptr[i+c] < 0x80) ? ptr[i+c] : '.';
fputc(x, stream);
}
fputc('\n', stream); /* newline */
}
}
static int
my_trace(CURL *handle, curl_infotype type, char *data, size_t size,void *userp)
{
const char *text;
(void)handle; /* prevent compiler warning */
(void)userp;
switch (type) {
case CURLINFO_TEXT:
fprintf(stderr, "== Info: %s", data);
default: /* in case a new one is introduced to shock us */
return 0;
case CURLINFO_HEADER_OUT:
text = "=> Send header";
break;
case CURLINFO_DATA_OUT:
text = "=> Send data";
break;
case CURLINFO_SSL_DATA_OUT:
text = "=> Send SSL data";
break;
case CURLINFO_HEADER_IN:
text = "<= Recv header";
break;
case CURLINFO_DATA_IN:
text = "<= Recv data";
break;
case CURLINFO_SSL_DATA_IN:
text = "<= Recv SSL data";
break;
}
dump(text, stderr, (unsigned char *)data, size);
return 0;
}
#if 0
static char*
urlify(NC_HTTP_STATE* state, const char* path)
{
NCbytes* buf = ncbytesnew();
char* tmp = NULL;
tmp = ncuribuild(state->url,NULL,NULL,NCURIPWD);
ncbytescat(buf,tmp);
nullfree(tmp); tmp = NULL;
ncbytescat(buf,"/");
if(state->url->path != NULL) {
if(state->url->path[0] == '/')
ncbytescat(buf,state->url->path+1);
else
ncbytescat(buf,state->url->path);
if(ncbytesget(buf,ncbyteslength(buf)-1) == '/')
ncbytessetlength(buf,ncbyteslength(buf)-1);
}
if(path != NULL) {
if(path[0] != '/')
ncbytescat(buf,"/");
ncbytescat(buf,path);
}
tmp = ncbytesextract(buf);
ncbytesfree(buf);
return tmp;
}
int
nc_http_urisplit(const char* url, char** rootp, char** pathp)
{
int stat = NC_NOERR;
NCURI* uri = NULL;
ncuriparse(url,&uri);
if(uri == NULL) {stat = NCTHROW(NC_EURL); goto done;}
if(rootp) {
char* tmp = ncuribuild(uri,NULL,NULL,NCURIPWD);
*rootp = tmp;
nullfree(tmp);
tmp = NULL;
}
if(pathp) {*pathp = strdup(uri->path);}
done:
return NCTHROW(stat);
}
#endif