~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbms/src/cslib/CSHTTPStream.cc

  • Committer: Lee Bieber
  • Date: 2010-10-22 16:47:38 UTC
  • mfrom: (1841.1.7 drizzle_pbms)
  • Revision ID: kalebral@gmail.com-20101022164738-vv8w22b8towpb307
Merge Barry - fix bug 657830: PBMS build failure in GCC 4.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
#include "CSConfig.h"
28
28
 
29
29
#include <stdlib.h>
 
30
#include <string.h>
30
31
#include <inttypes.h>
31
32
 
32
 
#include "string.h"
33
 
 
 
33
#include "CSGlobal.h"
34
34
#include "CSHTTPStream.h"
35
 
#include "CSGlobal.h"
 
35
#include "CSLog.h"
36
36
 
37
37
#ifdef DEBUG
38
38
//#define PRINT_HEADER
85
85
        iValue = value;
86
86
}
87
87
 
88
 
void CSHeader::write(CSOutputStream *out)
 
88
void CSHeader::write(CSOutputStream *out, bool trace)
89
89
{
 
90
        if (trace)
 
91
                printf("%s: %s\n", iName->getCString(), iValue->getCString());
 
92
 
90
93
        out->print(iName);
91
94
        out->print(": ");
92
 
        out->print(iValue);
 
95
        if (iValue)
 
96
                out->print(iValue);
93
97
        out->print("\r\n");
94
98
}
95
99
 
218
222
        exit_();
219
223
}
220
224
 
 
225
void CSHTTPHeaders::addHeader(const char *name, uint64_t value)
 
226
{
 
227
        char buffer[30];
 
228
 
 
229
        snprintf(buffer, 30, "%"PRIu64, value);
 
230
        addHeader(name, buffer);
 
231
}
 
232
 
221
233
void CSHTTPHeaders::removeHeader(CSString *name)
222
234
{
223
235
        enter_();
245
257
 
246
258
CSString *CSHTTPHeaders::getHeaderValue(const char *name)
247
259
{
248
 
        CSString *n;
249
260
        CSString *v;
250
261
 
251
 
        enter_();
252
 
        n = CSString::newString(name);
253
 
        push_(n);
254
262
        v = NULL;
255
263
        if (iHeaders) {
256
264
                CSHeader *h;
257
265
 
258
266
                for (uint32_t i=0; i<iHeaders->size(); i++) {
259
267
                        h = (CSHeader *) iHeaders->get(i);
260
 
                        if (h->getName()->compare(n) == 0) {
 
268
                        if (h->getName()->compare(name) == 0) {
261
269
                                v = h->getValue();
262
270
                                v->retain();
263
271
                                break;
264
272
                        }
265
273
                }
266
274
        }
267
 
        release_(n);
268
 
        return_(v);
269
 
}
270
 
 
271
 
void CSHTTPHeaders::writeHeader(CSOutputStream *out)
272
 
{
273
 
        if (iHeaders) {
274
 
                CSHeader *h;
275
 
 
276
 
                for (uint32_t i=0; i<iHeaders->size(); i++) {
277
 
                        h = (CSHeader *) iHeaders->get(i);
278
 
                        h->write(out);
 
275
        return v;
 
276
}
 
277
 
 
278
const char *CSHTTPHeaders::getHeaderCStringValue(const char *name)
 
279
{
 
280
        if (iHeaders) {
 
281
                CSHeader *h;
 
282
 
 
283
                for (uint32_t i=0; i<iHeaders->size(); i++) {
 
284
                        h = (CSHeader *) iHeaders->get(i);
 
285
                        if (h->getName()->compare(name) == 0) {
 
286
                                return h->getValue()->getCString();
 
287
                        }
 
288
                }
 
289
        }
 
290
        return NULL;
 
291
}
 
292
 
 
293
void CSHTTPHeaders::writeHeader(CSOutputStream *out, bool trace)
 
294
{
 
295
        if (iHeaders) {
 
296
                CSHeader *h;
 
297
 
 
298
                for (uint32_t i=0; i<iHeaders->size(); i++) {
 
299
                        h = (CSHeader *) iHeaders->get(i);
 
300
                        h->write(out, trace);
279
301
                }
280
302
        }
281
303
}
318
340
                iInput->release();
319
341
}
320
342
 
321
 
void CSHTTPInputStream::readHead()
 
343
void CSHTTPInputStream::readHead(bool trace)
322
344
{
323
345
        CSStringBuffer  *sb = NULL;
324
346
        bool                    first_line = true;
330
352
                sb = iInput->readLine();
331
353
                if (!sb)
332
354
                        break;
333
 
#ifdef PRINT_HEADER
334
 
                printf("HTTP: %s\n", sb->getCString());
335
 
#endif
 
355
                if (trace) {
 
356
                        if (first_line)
 
357
                                CSL.log(self, CSLog::Protocol, "HTTP Request - Header:\n");
 
358
                        printf("%s\n", sb->getCString());
 
359
                }
336
360
                if (sb->length() == 0) {
337
361
                        sb->release();
338
362
                        break;
401
425
        exit_();
402
426
}
403
427
 
 
428
void CSHTTPInputStream::readBody()
 
429
{
 
430
        uint64_t        body_size;
 
431
        size_t  tfer, len;
 
432
 
 
433
        if (getContentLength(&body_size)) {
 
434
                iBody.setLength((size_t) body_size);
 
435
                len = 0;
 
436
                while (len < body_size) {
 
437
                        tfer =  read(iBody.getBuffer(len), (size_t)(body_size - len));
 
438
                        if (!tfer)
 
439
                                CSException::throwException(CS_CONTEXT, CS_ERR_BODY_INCOMPLETE, "POST data incomplete");
 
440
                        len += tfer;
 
441
                }
 
442
        }
 
443
        else {
 
444
                CSStringBuffer *sb = NULL;
 
445
 
 
446
                /* Read until we have an empty line. */
 
447
                for (;;) {
 
448
                        sb = readLine();
 
449
                        if (!sb)
 
450
                                break;
 
451
                        if (sb->length() == 0) {
 
452
                                sb->release();
 
453
                                break;
 
454
                        }
 
455
                        iBody.append(sb->getBuffer(0), sb->length());
 
456
                        iBody.append((char) '\n');
 
457
                        sb->release();
 
458
                }
 
459
        }
 
460
}
 
461
 
404
462
bool CSHTTPInputStream::getRange(uint64_t *size, uint64_t *offset)
405
463
{
406
464
        CSString        *val;
423
481
        return haveRange;
424
482
}
425
483
 
426
 
uint64_t CSHTTPInputStream::getContentLength()
 
484
bool CSHTTPInputStream::getContentLength(uint64_t *length)
427
485
{
428
486
        CSString        *val;
429
487
        uint64_t                size = 0;
434
492
                if (len)  
435
493
                        sscanf(len, "%"PRIu64"", &size);                
436
494
                val->release();
 
495
                *length = size;
 
496
                return true;
437
497
        }
438
 
        return size;
 
498
        return false;
439
499
}
440
500
 
441
501
const char *CSHTTPInputStream::getMethod()
531
591
                iOutput->release();
532
592
}
533
593
 
534
 
void CSHTTPOutputStream::writeHeaders()
535
 
{
536
 
        writeHeader(this);
 
594
void CSHTTPOutputStream::print(const char *str, bool trace)
 
595
{
 
596
        if (trace)
 
597
                printf("%s", str);
 
598
        iOutput->print(str);
 
599
}
 
600
 
 
601
 
 
602
void CSHTTPOutputStream::print(int32_t value, bool trace)
 
603
{
 
604
        if (trace)
 
605
                printf("%d", value);
 
606
        iOutput->print(value);
 
607
}
 
608
 
 
609
void CSHTTPOutputStream::print(uint64_t value, bool trace)
 
610
{
 
611
        if (trace)
 
612
                printf("%"PRIu64, value);
 
613
        iOutput->print(value);
 
614
}
 
615
 
 
616
void CSHTTPOutputStream::writeHeaders(bool trace)
 
617
{
 
618
        writeHeader(this, trace);
537
619
        clearHeaders();
538
620
}
539
621
 
540
 
void CSHTTPOutputStream::writeHead()
 
622
void CSHTTPOutputStream::writeHead(bool trace)
541
623
{
542
 
        iOutput->print("HTTP/1.1 ");
543
 
        iOutput->print(iStatus);
544
 
        iOutput->print(" ");
545
 
        iOutput->print(getReasonPhrase(iStatus));
546
 
        iOutput->print("\r\n");
547
 
        writeHeader(iOutput);
548
 
        iOutput->print("Content-Length: ");
549
 
        iOutput->print(iContentLength);
550
 
        iOutput->print("\r\n");
 
624
        enter_();
 
625
        if (trace)
 
626
                CSL.log(self, CSLog::Protocol, "HTTP Reply - Header:\n");
 
627
        print("HTTP/1.1 ", trace);
 
628
        print(iStatus, trace);
 
629
        print(" ", trace);
 
630
        print(getReasonPhrase(iStatus), trace);
 
631
        print("\r\n", trace);
 
632
        writeHeader(iOutput, trace);
 
633
        print("Content-Length: ", trace);
 
634
        print(iContentLength, trace);
 
635
        print("\r\n", trace);
551
636
        if (iRangeSize && (iStatus == 200)) {
552
 
                iOutput->print("Content-Range: bytes ");
553
 
                iOutput->print(iRangeOffset);
554
 
                iOutput->print("-");
555
 
                iOutput->print(iRangeOffset + iRangeSize -1);
556
 
                iOutput->print("/");
557
 
                iOutput->print(iTotalLength);
558
 
                iOutput->print("\r\n");
 
637
                print("Content-Range: bytes ", trace);
 
638
                print(iRangeOffset, trace);
 
639
                print("-", trace);
 
640
                print(iRangeOffset + iRangeSize -1, trace);
 
641
                print("/", trace);
 
642
                print(iTotalLength, trace);
 
643
                print("\r\n", trace);
559
644
        }
560
 
        iOutput->print("\r\n");
 
645
        print("\r\n", trace);
 
646
        exit_();
 
647
 
561
648
}
562
649
 
563
650
void CSHTTPOutputStream::clearBody()
580
667
        iContentLength = iBody.length();
581
668
}
582
669
 
583
 
void CSHTTPOutputStream::appendBody(int value)
584
 
{
585
 
        iBody.append(value);
586
 
        iContentLength = iBody.length();
 
670
void CSHTTPOutputStream::appendBody(int32_t value)
 
671
{
 
672
        iBody.append(value);
 
673
        iContentLength = iBody.length();
 
674
}
 
675
 
 
676
void CSHTTPOutputStream::appendBody(uint32_t value)
 
677
{
 
678
        iBody.append(value);
 
679
        iContentLength = iBody.length();
 
680
}
 
681
 
 
682
void CSHTTPOutputStream::appendBody(uint64_t value)
 
683
{
 
684
        iBody.append(value);
 
685
        iContentLength = iBody.length();
 
686
}
 
687
 
 
688
const char *CSHTTPOutputStream::getBodyData()
 
689
{
 
690
        return iBody.getCString(); 
 
691
}
 
692
 
 
693
size_t CSHTTPOutputStream::getBodyLength()
 
694
{
 
695
        return iBody.length();
 
696
}
 
697
 
 
698
void CSHTTPOutputStream::setBody(CSStringBufferImpl *buf)
 
699
{
 
700
        iBody.take(buf);
 
701
        iContentLength = iBody.length(); 
587
702
}
588
703
 
589
704
void CSHTTPOutputStream::close()