HTTPClient for CyaSSL
Dependents: kintone-POST kintone-POST
Fork of HTTPClient by
Diff: HTTPClient.cpp
- Revision:
- 27:5d4739eae63e
- Parent:
- 22:4b9a4151cc73
- Child:
- 28:25b5d3720bd1
diff -r 531f03a263a9 -r 5d4739eae63e HTTPClient.cpp --- a/HTTPClient.cpp Sat Jul 12 12:09:38 2014 +0000 +++ b/HTTPClient.cpp Mon Jul 21 11:29:00 2014 +0000 @@ -114,6 +114,10 @@ ctx = 0 ; ssl = 0 ; SSLver = 3 ; + m_basicAuthUser = NULL ; + redirect_url = NULL ; + redirect = 0 ; + header = NULL ; } HTTPClient::~HTTPClient() @@ -163,11 +167,17 @@ return m_httpResponseCode; } -void HTTPClient::setHeader(char * h) +void HTTPClient::setHeader(const char * h) { header = h ; } +void HTTPClient::setLocationBuf(char * url, int size) +{ + redirect_url = url ; + redirect_url_size = size ; +} + HTTPResult HTTPClient::setSSLversion(int minorV) { if((minorV>=0) && (minorV<=3)) @@ -213,7 +223,8 @@ CYASSL_METHOD * SSLmethod ; m_httpResponseCode = 0; //Invalidate code m_timeout = timeout; - + redirect = 0 ; + pDataIn->writeReset(); if( pDataOut ) { pDataOut->readReset(); @@ -263,7 +274,7 @@ if(port == HTTPS_PORT) { /* Start SSL connect */ - DBG("SSLmethod=%d", SSLmethod) ; + DBG("SSLver=%d", SSLver) ; if(ctx == NULL) { switch(SSLver) { case 0 : SSLmethod = CyaSSLv3_client_method() ; break ; @@ -316,6 +327,9 @@ //Send default headers DBG("Sending headers"); + if(m_basicAuthUser) { + bAuth() ; /* send out Basic Auth header */ + } if( pDataOut != NULL ) { if( pDataOut->getIsChunked() ) { ret = send("Transfer-Encoding: chunked\r\n"); @@ -332,14 +346,11 @@ ret = send(buf); CHECK_CONN_ERR(ret); } - if(m_basicAuthUser) { - bAuth() ; /* send out Basic Auth header */ - } } //Add user headers if(header) { - ret = send(header); + ret = send((char *)header); CHECK_CONN_ERR(ret); } @@ -421,7 +432,7 @@ PRTCL_ERR(); } - if( (m_httpResponseCode < 200) || (m_httpResponseCode >= 300) ) { + if( (m_httpResponseCode < 200) || (m_httpResponseCode >= 400) ) { //Did not return a 2xx code; TODO fetch headers/(&data?) anyway and implement a mean of writing/reading headers WARN("Response code %d", m_httpResponseCode); PRTCL_ERR(); @@ -481,8 +492,11 @@ } } else if( !strcmp(key, "Content-Type") ) { pDataIn->setDataType(value); + } else if( !strcmp(key, "location") && redirect_url) { + sscanf(buf, "%31[^:]: %128[^\r\n]", key, redirect_url); + DBG("Redirect %s: %s", key, redirect_url) ; + redirect = 1 ; } - memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well trfLen -= (crlfPos + 2); @@ -584,8 +598,8 @@ cyassl_free() ; m_sock.close(); DBG("Completed HTTP transaction"); - - return HTTP_OK; + if(redirect)return HTTP_REDIRECT ; + else return HTTP_OK; } HTTPResult HTTPClient::recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen) //0 on success, err code on failure @@ -796,6 +810,7 @@ ret = send("Authorization: Basic ") ; CHECK_CONN_ERR(ret); sprintf(base64buff, "%s:%s", m_basicAuthUser, m_basicAuthPassword) ; + DBG("bAuth: %s", base64buff) ; base64enc(b_auth, base64buff) ; b_auth[strlen(b_auth)+1] = '\0' ; b_auth[strlen(b_auth)] = '\n' ;