@@ -54,8 +54,13 @@ int to_int(const std::string& str){
54
54
return numb;
55
55
}
56
56
57
- HTTP::HTTP (std::string uri, bool keepAlive): _keepAlive(keepAlive) {
58
-
57
+ HTTP::HTTP (std::string uri, bool keepAlive)
58
+ : _connection(0 ),
59
+ _sockfd(-1 ),
60
+ _keepAlive(keepAlive),
61
+ _keepAliveTimeout(60 ),
62
+ _lastRequest(0 )
63
+ {
59
64
// Remove http protocol if set.
60
65
size_t pos = uri.find (" http://" );
61
66
if ( pos != std::string::npos)
@@ -241,7 +246,9 @@ bool HTTP::request(const char* method, const char* endUrl, const char* data, Jso
241
246
}
242
247
243
248
try {
244
- jOutput->addMember (output.c_str (), output.c_str () + output.size ());
249
+ if (jOutput && output.size ()) {
250
+ jOutput->addMember (output.c_str (), output.c_str () + output.size ());
251
+ }
245
252
}
246
253
catch (Exception& e){
247
254
printf (" parser() failed in Getter. Exception caught: %s\n " , e.what ());
@@ -265,7 +272,7 @@ bool HTTP::sendMessage(const char* method, const char* endUrl, const char* data,
265
272
// Make the request type.
266
273
std::string requestString (method);
267
274
268
- assert (requestString == " POST" || requestString == " DELETE" || requestString == " GET" || requestString == " PUT" );
275
+ assert (requestString == " POST" || requestString == " DELETE" || requestString == " GET" || requestString == " PUT" || requestString == " HEAD " );
269
276
270
277
// Concatenate the page.
271
278
requestString += std::string (" " );
@@ -456,20 +463,20 @@ bool HTTP::readMessage(std::string& output) {
456
463
// Need to loop (recursion may fail because pile up over the stack for large requests.
457
464
size_t contentLength = 0 ;
458
465
bool isChunked = false ;
459
- int readResult = 1 ;
460
- while (readResult > 0 )
466
+ int readResult;
467
+ do {
461
468
readResult = readMessage (output, contentLength, isChunked);
469
+ } while (readResult == MORE_DATA);
462
470
463
- // If 0, normal message end.
464
- if (readResult == 0 )
471
+ if (readResult == OK)
465
472
return true ;
466
473
467
474
// If not, there was an error somewhere.
468
475
return false ;
469
476
}
470
477
471
478
// Wait with select then start to read the message.
472
- int HTTP::readMessage (std::string& output, size_t & contentLength, bool & isChunked) {
479
+ Status HTTP::readMessage (std::string& output, size_t & contentLength, bool & isChunked) {
473
480
474
481
// / First, use select() with a timeout value to determine when the file descriptor is ready to be read.
475
482
assert ( !error () );
@@ -497,17 +504,17 @@ int HTTP::readMessage(std::string& output, size_t& contentLength, bool& isChunke
497
504
// Is error ?
498
505
if (ret < 0 ) {
499
506
disconnect ();
500
- return - 1 ;
507
+ return ERROR ;
501
508
}
502
509
503
510
// Is timeout ?
504
511
if (ret == 0 )
505
- return - 1 ;
512
+ return ERROR ;
506
513
507
514
// Check error on socket
508
515
if (FD_ISSET ( fd, &errorSet)) {
509
516
error ();
510
- return - 1 ;
517
+ return ERROR ;
511
518
}
512
519
513
520
// Is read ?
@@ -516,7 +523,7 @@ int HTTP::readMessage(std::string& output, size_t& contentLength, bool& isChunke
516
523
return parseMessage (output, contentLength, isChunked);
517
524
}
518
525
519
- return 0 ;
526
+ return OK ;
520
527
}
521
528
522
529
// Append char* to output.
@@ -555,7 +562,7 @@ size_t HTTP::appendChunk(std::string& output, char* msg, size_t msgSize) {
555
562
}
556
563
557
564
// Whole process to read the response from HTTP server.
558
- int HTTP::parseMessage (std::string& output, size_t & contentLength, bool & isChunked) {
565
+ Status HTTP::parseMessage (std::string& output, size_t & contentLength, bool & isChunked) {
559
566
560
567
// Socket is ready for reading.
561
568
char recvline[4096 ];
@@ -564,15 +571,15 @@ int HTTP::parseMessage(std::string& output, size_t& contentLength, bool& isChunk
564
571
if (readSize <= 0 ) {
565
572
566
573
if (!connect ())
567
- return - 1 ;
574
+ return ERROR ;
568
575
569
576
if ( readSize < 0 ) {
570
577
if (errno != (EWOULDBLOCK | EAGAIN) )
571
578
EXCEPTION (" read error on socket" );
572
579
errno = 0 ;
573
580
}
574
581
575
- return 1 ;
582
+ return MORE_DATA ;
576
583
}
577
584
578
585
assert (readSize <= 4095 );
@@ -591,7 +598,7 @@ int HTTP::parseMessage(std::string& output, size_t& contentLength, bool& isChunk
591
598
592
599
// Append the message to the output.
593
600
if ( contentLength == 0 ){
594
- return 0 ;
601
+ return OK ;
595
602
}
596
603
}
597
604
@@ -602,23 +609,23 @@ int HTTP::parseMessage(std::string& output, size_t& contentLength, bool& isChunk
602
609
603
610
if (endStatus == NULL ){
604
611
disconnect ();
605
- return - 1 ;
612
+ return ERROR ;
606
613
}
607
614
608
615
// Extract and interpret status line.
609
616
std::stringstream status ( std::string (recvline, endStatus) );
610
617
611
618
if (!status) {
612
619
disconnect ();
613
- return - 1 ;
620
+ return ERROR ;
614
621
}
615
622
616
623
std::string httpVersion;
617
624
status >> httpVersion;
618
625
619
626
if (httpVersion.substr (0 , 5 ) != " HTTP/" ) {
620
627
disconnect ();
621
- return - 1 ;
628
+ return ERROR ;
622
629
}
623
630
624
631
unsigned int statusCode = 0 ;
@@ -647,13 +654,13 @@ int HTTP::parseMessage(std::string& output, size_t& contentLength, bool& isChunk
647
654
case 400 :
648
655
std::cout << " Status: Bad Request, you must reconsidered your request." << std::endl;
649
656
disconnect ();
650
- return - 1 ;
657
+ return ERROR ;
651
658
652
659
// If forbidden, it's over.
653
660
case 403 :
654
661
std::cout << " Status: Forbidden, you must reconsidered your request." << std::endl;
655
662
disconnect ();
656
- return - 1 ;
663
+ return ERROR ;
657
664
658
665
// If 404 then check the message and continue to get the complete response.
659
666
case 404 :
@@ -665,19 +672,19 @@ int HTTP::parseMessage(std::string& output, size_t& contentLength, bool& isChunk
665
672
case 500 :
666
673
std::cout << " 500 but statusMessage is not \" Internal Server Error\" ." << std::endl;
667
674
disconnect ();
668
- return - 1 ;
675
+ return ERROR ;
669
676
// If unhandled state, return false.
670
677
default :
671
678
std::cout << " Weird status code: " << statusCode << std::endl;
672
679
disconnect ();
673
- return - 1 ;
680
+ return ERROR ;
674
681
}
675
682
676
683
// Extract and interpret the header.
677
684
char * endHeader = strstr (endStatus+2 ," \r\n\r\n " );
678
685
if (endHeader == NULL ){
679
686
disconnect ();
680
- return - 1 ;
687
+ return ERROR ;
681
688
}
682
689
size_t headerSize = endHeader + 4 - recvline;
683
690
@@ -694,17 +701,17 @@ int HTTP::parseMessage(std::string& output, size_t& contentLength, bool& isChunk
694
701
695
702
// If we did not get the size of the chunk read message as a chunk, go on reading.
696
703
if (readSize - headerSize <= 0 )
697
- return 1 ;
704
+ return MORE_DATA ;
698
705
699
706
// If the message content the length, parse the size.
700
707
contentLength = appendChunk (output, endHeader + 4 , readSize - headerSize);
701
708
702
709
if (contentLength == 0 )
703
- return 0 ;
710
+ return OK ;
704
711
705
712
} else {
706
713
disconnect ();
707
- return - 1 ;
714
+ return ERROR ;
708
715
}
709
716
}
710
717
// We received the content-length.
@@ -713,7 +720,7 @@ int HTTP::parseMessage(std::string& output, size_t& contentLength, bool& isChunk
713
720
714
721
// Error due to conversion may set errno.
715
722
if (error ())
716
- return - 1 ;
723
+ return ERROR ;
717
724
718
725
// Copy content.
719
726
assert ( endHeader - recvline + contentLength + 4 >= (unsigned int )readSize);
@@ -727,25 +734,25 @@ int HTTP::parseMessage(std::string& output, size_t& contentLength, bool& isChunk
727
734
728
735
// When we did not receive the data yet. Wait with select.
729
736
if ( readSize == 0 )
730
- return 1 ;
737
+ return MORE_DATA ;
731
738
732
739
// When there is nothing more to read but the output is incomplete, wait with select.
733
740
if ( readSize < 0 ) {
734
741
735
742
if (errno != (EWOULDBLOCK | EAGAIN) )
736
743
EXCEPTION (" read error on socket" );
737
744
errno = 0 ;
738
- return 1 ;
745
+ return MORE_DATA ;
739
746
}
740
747
741
748
output.append (nextContent, readSize);
742
749
}
743
750
744
751
// If chunked but no size, we return until we receive the answer.
745
752
if (isChunked && contentLength == 0 )
746
- return 1 ;
753
+ return MORE_DATA ;
747
754
748
- return 0 ;
755
+ return OK ;
749
756
}
750
757
751
758
0 commit comments