Skip to content

Commit 02a08e6

Browse files
committed
Prepare for refactoring. Also some g++-4.6 (default g++ on Raspberry Pi) compile fixes
1 parent aab1299 commit 02a08e6

File tree

3 files changed

+55
-40
lines changed

3 files changed

+55
-40
lines changed

src/http/http.cpp

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,13 @@ int to_int(const std::string& str){
5454
return numb;
5555
}
5656

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+
{
5964
// Remove http protocol if set.
6065
size_t pos = uri.find("http://");
6166
if( pos != std::string::npos)
@@ -241,7 +246,9 @@ bool HTTP::request(const char* method, const char* endUrl, const char* data, Jso
241246
}
242247

243248
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+
}
245252
}
246253
catch(Exception& e){
247254
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,
265272
// Make the request type.
266273
std::string requestString(method);
267274

268-
assert(requestString == "POST" || requestString == "DELETE" || requestString == "GET" || requestString == "PUT");
275+
assert(requestString == "POST" || requestString == "DELETE" || requestString == "GET" || requestString == "PUT" || requestString == "HEAD");
269276

270277
// Concatenate the page.
271278
requestString += std::string(" ");
@@ -456,20 +463,20 @@ bool HTTP::readMessage(std::string& output) {
456463
// Need to loop (recursion may fail because pile up over the stack for large requests.
457464
size_t contentLength = 0;
458465
bool isChunked = false;
459-
int readResult = 1;
460-
while(readResult > 0)
466+
int readResult;
467+
do {
461468
readResult = readMessage(output, contentLength, isChunked);
469+
} while(readResult == MORE_DATA);
462470

463-
// If 0, normal message end.
464-
if(readResult == 0)
471+
if(readResult == OK)
465472
return true;
466473

467474
// If not, there was an error somewhere.
468475
return false;
469476
}
470477

471478
// 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) {
473480

474481
/// First, use select() with a timeout value to determine when the file descriptor is ready to be read.
475482
assert( !error() );
@@ -497,17 +504,17 @@ int HTTP::readMessage(std::string& output, size_t& contentLength, bool& isChunke
497504
// Is error ?
498505
if(ret < 0) {
499506
disconnect();
500-
return -1;
507+
return ERROR;
501508
}
502509

503510
// Is timeout ?
504511
if(ret == 0)
505-
return -1;
512+
return ERROR;
506513

507514
// Check error on socket
508515
if(FD_ISSET( fd, &errorSet)) {
509516
error();
510-
return -1;
517+
return ERROR;
511518
}
512519

513520
// Is read ?
@@ -516,7 +523,7 @@ int HTTP::readMessage(std::string& output, size_t& contentLength, bool& isChunke
516523
return parseMessage(output, contentLength, isChunked);
517524
}
518525

519-
return 0;
526+
return OK;
520527
}
521528

522529
// Append char* to output.
@@ -555,7 +562,7 @@ size_t HTTP::appendChunk(std::string& output, char* msg, size_t msgSize) {
555562
}
556563

557564
// 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) {
559566

560567
// Socket is ready for reading.
561568
char recvline[4096];
@@ -564,15 +571,15 @@ int HTTP::parseMessage(std::string& output, size_t& contentLength, bool& isChunk
564571
if(readSize <= 0) {
565572

566573
if(!connect())
567-
return -1;
574+
return ERROR;
568575

569576
if( readSize < 0 ) {
570577
if(errno != (EWOULDBLOCK | EAGAIN) )
571578
EXCEPTION("read error on socket");
572579
errno = 0;
573580
}
574581

575-
return 1;
582+
return MORE_DATA;
576583
}
577584

578585
assert(readSize <= 4095);
@@ -591,7 +598,7 @@ int HTTP::parseMessage(std::string& output, size_t& contentLength, bool& isChunk
591598

592599
// Append the message to the output.
593600
if( contentLength == 0 ){
594-
return 0;
601+
return OK;
595602
}
596603
}
597604

@@ -602,23 +609,23 @@ int HTTP::parseMessage(std::string& output, size_t& contentLength, bool& isChunk
602609

603610
if(endStatus == NULL){
604611
disconnect();
605-
return -1;
612+
return ERROR;
606613
}
607614

608615
// Extract and interpret status line.
609616
std::stringstream status( std::string(recvline, endStatus) );
610617

611618
if (!status) {
612619
disconnect();
613-
return -1;
620+
return ERROR;
614621
}
615622

616623
std::string httpVersion;
617624
status >> httpVersion;
618625

619626
if (httpVersion.substr(0, 5) != "HTTP/") {
620627
disconnect();
621-
return -1;
628+
return ERROR;
622629
}
623630

624631
unsigned int statusCode = 0;
@@ -647,13 +654,13 @@ int HTTP::parseMessage(std::string& output, size_t& contentLength, bool& isChunk
647654
case 400:
648655
std::cout << "Status: Bad Request, you must reconsidered your request." << std::endl;
649656
disconnect();
650-
return -1;
657+
return ERROR;
651658

652659
// If forbidden, it's over.
653660
case 403:
654661
std::cout << "Status: Forbidden, you must reconsidered your request." << std::endl;
655662
disconnect();
656-
return -1;
663+
return ERROR;
657664

658665
// If 404 then check the message and continue to get the complete response.
659666
case 404:
@@ -665,19 +672,19 @@ int HTTP::parseMessage(std::string& output, size_t& contentLength, bool& isChunk
665672
case 500:
666673
std::cout << " 500 but statusMessage is not \"Internal Server Error\"." << std::endl;
667674
disconnect();
668-
return -1;
675+
return ERROR;
669676
// If unhandled state, return false.
670677
default:
671678
std::cout << "Weird status code: " << statusCode << std::endl;
672679
disconnect();
673-
return -1;
680+
return ERROR;
674681
}
675682

676683
// Extract and interpret the header.
677684
char* endHeader = strstr(endStatus+2,"\r\n\r\n");
678685
if(endHeader == NULL){
679686
disconnect();
680-
return -1;
687+
return ERROR;
681688
}
682689
size_t headerSize = endHeader + 4 - recvline;
683690

@@ -694,17 +701,17 @@ int HTTP::parseMessage(std::string& output, size_t& contentLength, bool& isChunk
694701

695702
// If we did not get the size of the chunk read message as a chunk, go on reading.
696703
if(readSize - headerSize <= 0)
697-
return 1;
704+
return MORE_DATA;
698705

699706
// If the message content the length, parse the size.
700707
contentLength = appendChunk(output, endHeader + 4, readSize - headerSize);
701708

702709
if(contentLength == 0)
703-
return 0;
710+
return OK;
704711

705712
} else {
706713
disconnect();
707-
return -1;
714+
return ERROR;
708715
}
709716
}
710717
// We received the content-length.
@@ -713,7 +720,7 @@ int HTTP::parseMessage(std::string& output, size_t& contentLength, bool& isChunk
713720

714721
// Error due to conversion may set errno.
715722
if(error())
716-
return -1;
723+
return ERROR;
717724

718725
// Copy content.
719726
assert( endHeader - recvline + contentLength + 4 >= (unsigned int)readSize);
@@ -727,25 +734,25 @@ int HTTP::parseMessage(std::string& output, size_t& contentLength, bool& isChunk
727734

728735
// When we did not receive the data yet. Wait with select.
729736
if( readSize == 0 )
730-
return 1;
737+
return MORE_DATA;
731738

732739
// When there is nothing more to read but the output is incomplete, wait with select.
733740
if( readSize < 0 ) {
734741

735742
if(errno != (EWOULDBLOCK | EAGAIN) )
736743
EXCEPTION("read error on socket");
737744
errno = 0;
738-
return 1;
745+
return MORE_DATA;
739746
}
740747

741748
output.append(nextContent, readSize);
742749
}
743750

744751
// If chunked but no size, we return until we receive the answer.
745752
if(isChunked && contentLength == 0)
746-
return 1;
753+
return MORE_DATA;
747754

748-
return 0;
755+
return OK;
749756
}
750757

751758

src/http/http.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Exception : std::exception {
3939
public:
4040
template<typename T>
4141
Exception(const char* fil, int lin, T const& msg);
42+
virtual ~Exception() throw() {}
4243

4344
const char* what() const throw() { return _msg.c_str(); }
4445

@@ -54,6 +55,12 @@ Exception::Exception(const char* fil, int lin, T const& msg) {
5455
std::cerr << msg << std::endl;
5556
}
5657

58+
enum Status {
59+
OK,
60+
ERROR,
61+
MORE_DATA
62+
};
63+
5764
class HTTP {
5865
public:
5966
HTTP(std::string url, bool keepAlive);
@@ -111,10 +118,10 @@ class HTTP {
111118
bool readMessage(std::string& output);
112119

113120
/// Wait with select then start to read the message.
114-
int readMessage(std::string& output, size_t& contentLength, bool& isChunked);
121+
Status readMessage(std::string& output, size_t& contentLength, bool& isChunked);
115122

116123
/// Methods to read chunked messages.
117-
int parseMessage(std::string& output, size_t& contentLength, bool& isChunked) ;
124+
Status parseMessage(std::string& output, size_t& contentLength, bool& isChunked) ;
118125

119126
/// Append the chunk message to the stream.
120127
size_t appendChunk(std::string& output, char* msg, size_t msgSize);
@@ -128,12 +135,12 @@ class HTTP {
128135
std::string _url;
129136
std::string _urn;
130137
int _port;
131-
unsigned int _connection = 0;
132-
int _sockfd = -1;
138+
unsigned int _connection;
139+
int _sockfd;
133140
struct sockaddr_in _client;
134-
bool _keepAlive = false;
135-
time_t _keepAliveTimeout = 60;
136-
time_t _lastRequest = 0;
141+
bool _keepAlive;
142+
time_t _keepAliveTimeout;
143+
time_t _lastRequest;
137144

138145
/// Mutex for every request.
139146
std::mutex _requestMutex;

src/json/json.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <iostream>
2525
#include <sstream>
2626
#include <cstring>
27+
#include <stdexcept>
2728

2829
#define BACKSLASH 0x5c
2930

0 commit comments

Comments
 (0)