Mixe for Privacy and Anonymity in the Internet
Public Member Functions | Private Attributes | List of all members
CAHttpClient Class Reference

Very simple http client. More...

#include <CAHttpClient.hpp>

Collaboration diagram for CAHttpClient:

Public Member Functions

 CAHttpClient (CASocket *pSocket)
 
 CAHttpClient ()
 
 ~CAHttpClient ()
 
SINT32 setSocket (CASocket *pSocket)
 
SINT32 sendGetRequest (const UINT8 *url)
 Sends a HTTP GET request to the server. More...
 
SINT32 sendPostRequest (const UINT8 *url, const UINT8 *data, const UINT32 dataLen)
 Sends a HTTP POST request to the server. More...
 
SINT32 parseHTTPHeader (UINT32 *contentLength, UINT32 *statusCode=NULL, UINT32 msTimeOut=3000)
 receives the HTTP header and parses the content length More...
 
SINT32 getContent (UINT8 *a_pContent, UINT32 *a_pLength)
 Retruns the content of the response. More...
 

Private Attributes

CASocketm_pSocket
 the socket connection to the http server More...
 

Detailed Description

Very simple http client.

Used by CAInfoService and CAAccountingBIInterface. Must be initialized with a connected socket. Note that the socket is still owned by the caller. CAHttpClient will not delete the socket

Author
Bastian Voigt

Definition at line 43 of file CAHttpClient.hpp.

Constructor & Destructor Documentation

◆ CAHttpClient() [1/2]

CAHttpClient::CAHttpClient ( CASocket pSocket)
inline

Definition at line 46 of file CAHttpClient.hpp.

47  {
48  m_pSocket = pSocket;
49  }
CASocket * m_pSocket
the socket connection to the http server

References m_pSocket.

◆ CAHttpClient() [2/2]

CAHttpClient::CAHttpClient ( )
inline

Definition at line 51 of file CAHttpClient.hpp.

52  {
53  m_pSocket = NULL;
54  }

References m_pSocket.

◆ ~CAHttpClient()

CAHttpClient::~CAHttpClient ( )
inline

Definition at line 56 of file CAHttpClient.hpp.

56 {}

Member Function Documentation

◆ getContent()

SINT32 CAHttpClient::getContent ( UINT8 a_pContent,
UINT32 a_pLength 
)

Retruns the content of the response.

Gets the content of a HTTP response.

Parameters
a_pContentbuff which receives the content
a_pLengthon input contains the size of a_pContent, on return contains the number of received bytes
Return values
E_NOT_CONNECTEDif socket is not connected
E_SUCCESSif successful

Definition at line 234 of file CAHttpClient.cpp.

235  {
236  if(m_pSocket==NULL)
237  {
238  return E_NOT_CONNECTED;
239  }
240 
241  UINT32 aktIndex = 0;
242  UINT32 len=*a_pLength;
243  while(len>0)
244  {
245  SINT32 ret=m_pSocket->receive(a_pContent+aktIndex,len);
246  if(ret<=0)
247  break;
248  aktIndex+=ret;
249  len-=ret;
250  }
251  *a_pLength=aktIndex;
252  return E_SUCCESS;
253  }
signed int SINT32
Definition: basetypedefs.h:132
unsigned int UINT32
Definition: basetypedefs.h:131
virtual SINT32 receive(UINT8 *buff, UINT32 len)
Will receive some bytes from the socket.
Definition: CASocket.cpp:645
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_NOT_CONNECTED
Definition: errorcodes.hpp:22
UINT16 len
Definition: typedefs.hpp:0

References E_NOT_CONNECTED, E_SUCCESS, len, m_pSocket, and CASocket::receive().

Referenced by CAInfoService::getPaymentInstance().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parseHTTPHeader()

SINT32 CAHttpClient::parseHTTPHeader ( UINT32 contentLength,
UINT32 statusCode = NULL,
UINT32  msTimeOut = 3000 
)

receives the HTTP header and parses the content length

Receives the HTTP header and parses the content length.

Parameters
contentLengthreceives the parsed content length
statusCodeif set, receives the http statuscode (200, 403, 404, ...)
Return values
E_SUCCESSif all is OK
E_UNKNOWNif the server returned a http errorcode TODO: Verify that "HTTP/1.1 200 OK" must be the first line!
Parameters
contentLengthreceives the parsed content length
statusCodeif set, receives the http statuscode (200, 403, 404, ...)
msTimeOuttime in ms for receiving the HTTPHeader (<=0 means no timeout)
Return values
E_SUCCESSif all is OK
E_NOT_CONNECTEDif the connection to the Web-Server was lost
E_UNKNOWNif the server returned a http errorcode, in this case statusCode is set to 0 and contentLength is set to 0
Todo:

: Verify that "HTTP/1.1 200 OK" must be the first line!

: Maybe set an other statusCode in case of an error ?

TODO: do it better (case insensitive compare!)

Definition at line 128 of file CAHttpClient.cpp.

129  {
130  if(contentLength!=NULL)
131  *contentLength=0;
132  if(statusCode!=NULL)
133  *statusCode=0;
134  char *line = new char[255];
135  SINT32 ret = 0;
136  SINT32 ret2 = E_UNKNOWN;
137 
138  UINT64 currentTime = 0, endTime = 0;
139  bool nbl = false;
140 
141  getcurrentTimeMillis(currentTime);
142  set64(endTime,currentTime);
143  add64(endTime,msTimeOut);
144 
145  if(msTimeOut > 0)
146  {
147  m_pSocket->getNonBlocking(&nbl);
148  m_pSocket->setNonBlocking(true);
149  }
150 
151  if(!m_pSocket)
152  {
153  return E_NOT_CONNECTED;
154  }
155  do
156  {
157  int i=0;
158  UINT8 byte = 0;
159  do//Read a line from the WebServer
160  {
161 
162 receiving:
163  ret = m_pSocket->receive(&byte, 1); //bytewise? ugh!
164  if( (ret == E_AGAIN) && (msTimeOut > 0) )
165  {
166  getcurrentTimeMillis(currentTime);
167  if(!isLesser64(currentTime,endTime))
168  {
169  CAMsg::printMsg(LOG_ERR, "HTTP-Client parseHeader: timeout!\n");
170  ret = E_TIMEDOUT;
171  ret2 = E_TIMEDOUT;
172  break;
173  }
174  msTimeOut=diff64(endTime,currentTime);
175  msSleep(50);
176  goto receiving;
177  }
178  if(byte == '\r' || byte == '\n')
179  {
180  line[i++] = 0;
181  }
182  else
183  {
184  line[i++] = byte;
185  }
186  }
187  while(byte != '\n' && i<255 && ret > 0 );
188 
189  if(ret < 0||i>=255)
190  {
191  CAMsg::printMsg(LOG_CRIT,"HttpClient: Error return code: %i.\n",ret);
192  break;
193  }
194  if(strncmp(line, "HTTP", 4) == 0)
195  {
196  if(statusCode!=NULL && strlen(line)>9) // parse statusCode
197  {
198  *statusCode = atoi(line+9);
199  }
200  if(strstr(line, "200 OK") == NULL)
201  {
202  CAMsg::printMsg(LOG_CRIT,"HttpClient: Error: Server returned: '%s'.\n",line);
203  break;
204  }
205  else
206  ret2=E_SUCCESS;
207  }
209  else if( (strncmp(line, "Content-length: ", 16) == 0) ||
210  (strncmp(line, "Content-Length: ", 16) == 0))
211  {
212  *contentLength = (UINT32) atol(line+16);
213  }
214  #ifdef DEBUG
215  CAMsg::printMsg(LOG_DEBUG,"Server returned: '%s'.\n",line);
216  #endif
217  }
218  while(strlen(line) > 0);//Stop reading of response lines, if an empty line was reveived...
219  if(msTimeOut > 0)
220  {
222  }
223  delete[] line;
224  line = NULL;
225  return ret2;
226  }
SINT32 getcurrentTimeMillis(UINT64 &u64Time)
Gets the current Systemtime in milli seconds.
Definition: CAUtil.cpp:252
SINT32 msSleep(UINT32 ms)
Sleeps ms milliseconds.
Definition: CAUtil.cpp:406
UINT32 diff64(const UINT64 &bigop, const UINT64 &smallop)
Definition: CAUtil.hpp:398
void add64(UINT64 &op1, UINT32 op2)
Definition: CAUtil.hpp:375
bool isLesser64(UINT64 &smallOp1, UINT64 &bigOp2)
Definition: CAUtil.hpp:442
void set64(UINT64 &op1, UINT32 op2)
Definition: CAUtil.hpp:321
unsigned char UINT8
Definition: basetypedefs.h:135
static SINT32 printMsg(UINT32 typ, const char *format,...)
Writes a given message to the log.
Definition: CAMsg.cpp:251
virtual SINT32 getNonBlocking(bool *b)
Definition: CASocket.cpp:972
virtual SINT32 setNonBlocking(bool b)
Definition: CASocket.cpp:947
#define E_AGAIN
Definition: errorcodes.hpp:9
#define E_UNKNOWN
Definition: errorcodes.hpp:3
#define E_TIMEDOUT
Definition: errorcodes.hpp:10

References add64(), diff64(), E_AGAIN, E_NOT_CONNECTED, E_SUCCESS, E_TIMEDOUT, E_UNKNOWN, getcurrentTimeMillis(), CASocket::getNonBlocking(), isLesser64(), m_pSocket, msSleep(), CAMsg::printMsg(), CASocket::receive(), set64(), and CASocket::setNonBlocking().

Referenced by CAInfoService::getPaymentInstance(), CAInfoService::sendMixHelo(), CAAccountingBIInterface::settle(), and CAAccountingBIInterface::settleAll().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendGetRequest()

SINT32 CAHttpClient::sendGetRequest ( const UINT8 url)

Sends a HTTP GET request to the server.

Parameters
urlthe local part of the URL requested (e.g. "/settle")
Returns
E_UNKNOWN on socket errors
E_NOT_CONNECTED if the connection was lost
E_SUCCESS if all is OK
Parameters
urlthe local part of the URL requested (e.g. "/settle")
Return values
E_UNKNOWNon socket errors
E_NOT_CONNECTEDif the connection was lost
E_SUCCESSif all is OK

Definition at line 39 of file CAHttpClient.cpp.

40  {
41  static const char* requestF = "GET %s HTTP/1.0\r\n\r\n";
42  static const UINT32 requestFLen=strlen(requestF);
43  if(m_pSocket==NULL)
44  {
45  return E_NOT_CONNECTED;
46  }
47 
48  // put request together
49  UINT32 len = requestFLen + strlen((char *)url);
50  UINT8* requestS = new UINT8[len+1];
51  sprintf((char *)requestS, requestF, (const char *)url);
52  len = strlen((char *)requestS);
53 
54  #ifdef DEBUG
55  CAMsg::printMsg(LOG_DEBUG, "HttpClient now sending: %s\n", requestS);
56  #endif
57 
58  // send it
59  SINT32 ret = m_pSocket->sendFullyTimeOut(requestS,len, 1000, 1000);
60  delete[] requestS;
61  requestS = NULL;
62  if(ret != E_SUCCESS)
63  { // socket error
64  return E_UNKNOWN;
65  }
66  return E_SUCCESS;
67  }
virtual SINT32 sendFullyTimeOut(const UINT8 *buff, UINT32 len, UINT32 msTimeOut, UINT32 msTimeOutSingleSend)
Sends all data over the network.
Definition: CASocket.cpp:488

References E_NOT_CONNECTED, E_SUCCESS, E_UNKNOWN, len, m_pSocket, CAMsg::printMsg(), and CASocket::sendFullyTimeOut().

Referenced by CAInfoService::getPaymentInstance().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendPostRequest()

SINT32 CAHttpClient::sendPostRequest ( const UINT8 url,
const UINT8 data,
const UINT32  dataLen 
)

Sends a HTTP POST request to the server.

Parameters
urlthe local part of the requested URL (e.g. "/settle")
dataa buffer containing the data
dataLenthe length of the data in bytes
Returns
E_UNKNOWN on socket errors
E_NOT_CONNECTED if the connection was lost
E_SUCCESS if all is OK

Definition at line 81 of file CAHttpClient.cpp.

82  {
83  UINT8 requestF[] = "POST %s HTTP/1.0\r\nContent-length: %u\r\n\r\n";
84  UINT32 len;
85  UINT8* requestS;
86 
87  if(!m_pSocket)
88  {
89  return E_NOT_CONNECTED;
90  }
91 
92  // put request together
93  len = strlen((char *)requestF) + strlen((char *)url) + 30;
94  requestS=new UINT8[len+1];
95  sprintf((char *)requestS, (char *)requestF, (char *)url, dataLen);
96  len = strlen((char *)requestS);
97  #ifdef DEBUG
98  CAMsg::printMsg(LOG_DEBUG, "HttpClient now sending: %s\n", requestS);
99  #endif
100  SINT32 ret=m_pSocket->sendFullyTimeOut(requestS,len, 1000, 1000);
101  //SINT32 ret=m_pSocket->sendFully(requestS,len);
102  delete[] requestS;
103  requestS = NULL;
104  if(ret!=E_SUCCESS)
105  {
106  return E_UNKNOWN;
107  }
108  ret=m_pSocket->sendFullyTimeOut(data,dataLen, 2000, 1000);
109  //ret=m_pSocket->sendFully(data,dataLen);
110  if(ret != E_SUCCESS)
111  { // socket error
112  return E_UNKNOWN;
113  }
114  return E_SUCCESS;
115  }
UINT8 data[PAYLOAD_SIZE]
Definition: typedefs.hpp:2

References data, E_NOT_CONNECTED, E_SUCCESS, E_UNKNOWN, len, m_pSocket, CAMsg::printMsg(), and CASocket::sendFullyTimeOut().

Referenced by CAAccountingBIInterface::settle(), and CAAccountingBIInterface::settleAll().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setSocket()

SINT32 CAHttpClient::setSocket ( CASocket pSocket)
inline

Definition at line 58 of file CAHttpClient.hpp.

59  {
60  m_pSocket = pSocket;
61  return E_SUCCESS;
62  }

References E_SUCCESS, and m_pSocket.

Referenced by CAInfoService::getPaymentInstance(), CAAccountingBIInterface::initBIConnection(), and CAInfoService::sendMixHelo().

Here is the caller graph for this function:

Member Data Documentation

◆ m_pSocket

CASocket* CAHttpClient::m_pSocket
private

the socket connection to the http server

Definition at line 102 of file CAHttpClient.hpp.

Referenced by CAHttpClient(), getContent(), parseHTTPHeader(), sendGetRequest(), sendPostRequest(), and setSocket().


The documentation for this class was generated from the following files: