~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: lbieber
  • Date: 2010-10-06 16:34:16 UTC
  • mfrom: (1816.1.3 build)
  • Revision ID: lbieber@orisndriz08-20101006163416-ea0sl59qgpglk21y
Merge Monty - Change the requirement from either libinnodb to libhaildb. Also, tied it to version 2.2
Merge Andrew - fix bug 650935: remove --compress from all clients
Merge Andrew - fix bug 653471: Add -A to drizzle client
Merge Travis - 621861 = To change C structs to C++ classes in Drizzle

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
 
1
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
2
2
 *
3
3
 * PrimeBase Media Stream for MySQL
4
4
 *
27
27
#include "CSConfig.h"
28
28
 
29
29
#include <stdlib.h>
30
 
#include <string.h>
31
30
#include <inttypes.h>
32
31
 
 
32
#include "string.h"
 
33
 
 
34
#include "CSHTTPStream.h"
33
35
#include "CSGlobal.h"
34
 
#include "CSHTTPStream.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, bool trace)
 
88
void CSHeader::write(CSOutputStream *out)
89
89
{
90
 
        if (trace)
91
 
                printf("%s: %s\n", iName->getCString(), iValue->getCString());
92
 
 
93
90
        out->print(iName);
94
91
        out->print(": ");
95
 
        if (iValue)
96
 
                out->print(iValue);
 
92
        out->print(iValue);
97
93
        out->print("\r\n");
98
94
}
99
95
 
222
218
        exit_();
223
219
}
224
220
 
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
 
 
233
221
void CSHTTPHeaders::removeHeader(CSString *name)
234
222
{
235
223
        enter_();
257
245
 
258
246
CSString *CSHTTPHeaders::getHeaderValue(const char *name)
259
247
{
 
248
        CSString *n;
260
249
        CSString *v;
261
250
 
 
251
        enter_();
 
252
        n = CSString::newString(name);
 
253
        push_(n);
262
254
        v = NULL;
263
255
        if (iHeaders) {
264
256
                CSHeader *h;
265
257
 
266
258
                for (uint32_t i=0; i<iHeaders->size(); i++) {
267
259
                        h = (CSHeader *) iHeaders->get(i);
268
 
                        if (h->getName()->compare(name) == 0) {
 
260
                        if (h->getName()->compare(n) == 0) {
269
261
                                v = h->getValue();
270
262
                                v->retain();
271
263
                                break;
272
264
                        }
273
265
                }
274
266
        }
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);
 
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);
301
279
                }
302
280
        }
303
281
}
340
318
                iInput->release();
341
319
}
342
320
 
343
 
void CSHTTPInputStream::readHead(bool trace)
 
321
void CSHTTPInputStream::readHead()
344
322
{
345
323
        CSStringBuffer  *sb = NULL;
346
324
        bool                    first_line = true;
352
330
                sb = iInput->readLine();
353
331
                if (!sb)
354
332
                        break;
355
 
                if (trace) {
356
 
                        if (first_line)
357
 
                                CSL.log(self, CSLog::Protocol, "HTTP Request - Header:\n");
358
 
                        printf("%s\n", sb->getCString());
359
 
                }
 
333
#ifdef PRINT_HEADER
 
334
                printf("HTTP: %s\n", sb->getCString());
 
335
#endif
360
336
                if (sb->length() == 0) {
361
337
                        sb->release();
362
338
                        break;
425
401
        exit_();
426
402
}
427
403
 
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
 
 
462
404
bool CSHTTPInputStream::getRange(uint64_t *size, uint64_t *offset)
463
405
{
464
406
        CSString        *val;
481
423
        return haveRange;
482
424
}
483
425
 
484
 
bool CSHTTPInputStream::getContentLength(uint64_t *length)
 
426
uint64_t CSHTTPInputStream::getContentLength()
485
427
{
486
428
        CSString        *val;
487
429
        uint64_t                size = 0;
492
434
                if (len)  
493
435
                        sscanf(len, "%"PRIu64"", &size);                
494
436
                val->release();
495
 
                *length = size;
496
 
                return true;
497
437
        }
498
 
        return false;
 
438
        return size;
499
439
}
500
440
 
501
441
const char *CSHTTPInputStream::getMethod()
591
531
                iOutput->release();
592
532
}
593
533
 
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);
 
534
void CSHTTPOutputStream::writeHeaders()
 
535
{
 
536
        writeHeader(this);
619
537
        clearHeaders();
620
538
}
621
539
 
622
 
void CSHTTPOutputStream::writeHead(bool trace)
 
540
void CSHTTPOutputStream::writeHead()
623
541
{
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);
 
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");
636
551
        if (iRangeSize && (iStatus == 200)) {
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);
 
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");
644
559
        }
645
 
        print("\r\n", trace);
646
 
        exit_();
647
 
 
 
560
        iOutput->print("\r\n");
648
561
}
649
562
 
650
563
void CSHTTPOutputStream::clearBody()
667
580
        iContentLength = iBody.length();
668
581
}
669
582
 
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(); 
 
583
void CSHTTPOutputStream::appendBody(int value)
 
584
{
 
585
        iBody.append(value);
 
586
        iContentLength = iBody.length();
702
587
}
703
588
 
704
589
void CSHTTPOutputStream::close()