Added support for Vodafone K4606, for more details refer to http://atmega.magictale.com/2215/hacking-vodafone-k4606-3g-hsdpa-usb-modem/

Fork of HTTPClient by Donatien Garnier

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTTPClient.cpp Source File

HTTPClient.cpp

00001 /* HTTPClient.cpp */
00002 /* Copyright (C) 2012 mbed.org, MIT License
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00005  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00006  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in all copies or
00011  * substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00014  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018  */
00019 
00020 //Debug is disabled by default
00021 #if 0
00022 //Enable debug
00023 #include <cstdio>
00024 #define DBG(x, ...) std::printf("[HTTPClient : DBG]"x"\r\n", ##__VA_ARGS__); 
00025 #define WARN(x, ...) std::printf("[HTTPClient : WARN]"x"\r\n", ##__VA_ARGS__); 
00026 #define ERR(x, ...) std::printf("[HTTPClient : ERR]"x"\r\n", ##__VA_ARGS__); 
00027 
00028 #else
00029 //Disable debug
00030 #define DBG(x, ...) 
00031 #define WARN(x, ...)
00032 #define ERR(x, ...) 
00033 
00034 #endif
00035 
00036 #define KEY_VAL_BUF_SIZE 60
00037 
00038 #define HTTP_PORT 80
00039 
00040 #define OK 0
00041 
00042 #define MIN(x,y) (((x)<(y))?(x):(y))
00043 #define MAX(x,y) (((x)>(y))?(x):(y))
00044 
00045 #define CHUNK_SIZE 256
00046 
00047 #include <cstring>
00048 
00049 #include "HTTPClient.h"
00050 
00051 HTTPClient::HTTPClient() :
00052 m_sock(), m_basicAuthUser(NULL), m_basicAuthPassword(NULL), m_httpResponseCode(0)
00053 {
00054 
00055 }
00056 
00057 HTTPClient::~HTTPClient()
00058 {
00059 
00060 }
00061 
00062 #if 0
00063 void HTTPClient::basicAuth(const char* user, const char* password) //Basic Authentification
00064 {
00065   m_basicAuthUser = user;
00066   m_basicAuthPassword = password;
00067 }
00068 #endif
00069 
00070 HTTPResult HTTPClient::get(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
00071 {
00072   return connect(url, HTTP_GET, NULL, pDataIn, timeout);
00073 }
00074 
00075 HTTPResult HTTPClient::get(const char* url, char* result, size_t maxResultLen, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
00076 {
00077   HTTPText str(result, maxResultLen);
00078   return get(url, &str, timeout);
00079 }
00080 
00081 HTTPResult HTTPClient::post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
00082 {
00083   return connect(url, HTTP_POST, (IHTTPDataOut*)&dataOut, pDataIn, timeout);
00084 }
00085 
00086 HTTPResult HTTPClient::put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
00087 {
00088   return connect(url, HTTP_PUT, (IHTTPDataOut*)&dataOut, pDataIn, timeout);
00089 }
00090 
00091 HTTPResult HTTPClient::del(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking
00092 {
00093   return connect(url, HTTP_DELETE, NULL, pDataIn, timeout);
00094 }
00095 
00096 
00097 int HTTPClient::getHTTPResponseCode()
00098 {
00099   return m_httpResponseCode;
00100 }
00101 
00102 #define CHECK_CONN_ERR(ret) \
00103   do{ \
00104     if(ret) { \
00105       m_sock.close(); \
00106       ERR("Connection error (%d)", ret); \
00107       return HTTP_CONN; \
00108     } \
00109   } while(0)
00110 
00111 #define PRTCL_ERR() \
00112   do{ \
00113     m_sock.close(); \
00114     ERR("Protocol error"); \
00115     return HTTP_PRTCL; \
00116   } while(0)
00117 
00118 HTTPResult HTTPClient::connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout) //Execute request
00119 { 
00120   m_httpResponseCode = 0; //Invalidate code
00121   m_timeout = timeout;
00122   
00123   pDataIn->writeReset();
00124   if( pDataOut )
00125   {
00126     pDataOut->readReset();
00127   }
00128 
00129   char scheme[8];
00130   uint16_t port;
00131   char host[32];
00132   char path[64];
00133   //First we need to parse the url (http[s]://host[:port][/[path]]) -- HTTPS not supported (yet?)
00134   HTTPResult res = parseURL(url, scheme, sizeof(scheme), host, sizeof(host), &port, path, sizeof(path));
00135   if(res != HTTP_OK)
00136   {
00137     ERR("parseURL returned %d", res);
00138     return res;
00139   }
00140 
00141   if(port == 0) //TODO do handle HTTPS->443
00142   {
00143     port = 80;
00144   }
00145 
00146   DBG("Scheme: %s", scheme);
00147   DBG("Host: %s", host);
00148   DBG("Port: %d", port);
00149   DBG("Path: %s", path);
00150 
00151   //Connect
00152   DBG("Connecting socket to server");
00153   int ret = m_sock.connect(host, port);
00154   if (ret < 0)
00155   {
00156     m_sock.close();
00157     ERR("Could not connect");
00158     return HTTP_CONN;
00159   }
00160 
00161   //Send request
00162   DBG("Sending request");
00163   char buf[CHUNK_SIZE];
00164   const char* meth = (method==HTTP_GET)?"GET":(method==HTTP_POST)?"POST":(method==HTTP_PUT)?"PUT":(method==HTTP_DELETE)?"DELETE":"";
00165   snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s\r\n", meth, path, host); //Write request
00166   ret = send(buf);
00167   if(ret)
00168   {
00169     m_sock.close();
00170     ERR("Could not write request");
00171     return HTTP_CONN;
00172   }
00173 
00174   //Send all headers
00175 
00176   //Send default headers
00177   DBG("Sending headers");
00178   if( pDataOut != NULL )
00179   {
00180     if( pDataOut->getIsChunked() )
00181     {
00182       ret = send("Transfer-Encoding: chunked\r\n");
00183       CHECK_CONN_ERR(ret);
00184     }
00185     else
00186     {
00187       snprintf(buf, sizeof(buf), "Content-Length: %d\r\n", pDataOut->getDataLen());
00188       ret = send(buf);
00189       CHECK_CONN_ERR(ret);
00190     }
00191     char type[48];
00192     if( pDataOut->getDataType(type, 48) == HTTP_OK )
00193     {
00194       snprintf(buf, sizeof(buf), "Content-Type: %s\r\n", type);
00195       ret = send(buf);
00196       CHECK_CONN_ERR(ret);
00197     }
00198     
00199     //Send specific headers
00200     while( pDataOut->getHeader(buf, sizeof(buf) - 3) ) //must have space left for CRLF + 0 terminating char
00201     {
00202       size_t headerlen = strlen(buf);
00203       snprintf(buf + headerlen, sizeof(buf) - headerlen, "\r\n");
00204       ret = send(buf);
00205       CHECK_CONN_ERR(ret);
00206     }
00207   }
00208   
00209   //Send specific headers
00210   while( pDataIn->getHeader(buf, sizeof(buf) - 3) )
00211   {
00212     size_t headerlen = strlen(buf);
00213     snprintf(buf + headerlen, sizeof(buf) - headerlen, "\r\n");
00214     ret = send(buf);
00215     CHECK_CONN_ERR(ret);
00216   }
00217   
00218   //Close headers
00219   DBG("Headers sent");
00220   ret = send("\r\n");
00221   CHECK_CONN_ERR(ret);
00222 
00223   size_t trfLen;
00224   
00225   //Send data (if available)
00226   if( pDataOut != NULL )
00227   {
00228     DBG("Sending data");
00229     while(true)
00230     {
00231       size_t writtenLen = 0;
00232       pDataOut->read(buf, CHUNK_SIZE, &trfLen);
00233       if( pDataOut->getIsChunked() )
00234       {
00235         //Write chunk header
00236         char chunkHeader[16];
00237         snprintf(chunkHeader, sizeof(chunkHeader), "%X\r\n", trfLen); //In hex encoding
00238         ret = send(chunkHeader);
00239         CHECK_CONN_ERR(ret);
00240       }
00241       else if( trfLen == 0 )
00242       {
00243         break;
00244       }
00245       if( trfLen != 0 )
00246       {
00247         ret = send(buf, trfLen);
00248         CHECK_CONN_ERR(ret);
00249       }
00250 
00251       if( pDataOut->getIsChunked()  )
00252       {
00253         ret = send("\r\n"); //Chunk-terminating CRLF
00254         CHECK_CONN_ERR(ret);
00255       }
00256       else
00257       {
00258         writtenLen += trfLen;
00259         if( writtenLen >= pDataOut->getDataLen() )
00260         {
00261           break;
00262         }
00263       }
00264 
00265       if( trfLen == 0 )
00266       {
00267         break;
00268       }
00269     }
00270   }
00271   
00272   //Receive response
00273   DBG("Receiving response");
00274   ret = recv(buf, 1, CHUNK_SIZE - 1, &trfLen); //Read n bytes
00275   CHECK_CONN_ERR(ret);
00276 
00277   buf[trfLen] = '\0';
00278 
00279   //Make sure we got the first response line
00280   char* crlfPtr = NULL;
00281   while( true )
00282   {
00283     crlfPtr = strstr(buf, "\r\n");
00284   if(crlfPtr == NULL)
00285   {
00286       if( trfLen < CHUNK_SIZE - 1 )
00287       {
00288         size_t newTrfLen;
00289         ret = recv(buf + trfLen, 1, CHUNK_SIZE - trfLen - 1, &newTrfLen);
00290         trfLen += newTrfLen;
00291         buf[trfLen] = '\0';
00292         DBG("Read %d chars; In buf: [%s]", newTrfLen, buf);
00293         CHECK_CONN_ERR(ret);
00294         continue;
00295       }
00296       else
00297       {
00298     PRTCL_ERR();
00299   }
00300     }
00301     break;
00302   }
00303 
00304   int crlfPos = crlfPtr - buf;
00305   buf[crlfPos] = '\0';
00306 
00307   //Parse HTTP response
00308   //if( sscanf(buf, "HTTP/%*d.%*d %d %*[^\r\n]", &m_httpResponseCode) != 1 )
00309   if(crlfPos > 13)
00310   {
00311     buf[13] = '\0';
00312   }
00313   if( sscanf(buf, "HTTP/%*d.%*d %d", &m_httpResponseCode) != 1 ) //Kludge for newlib nano
00314   {
00315     //Cannot match string, error
00316     ERR("Not a correct HTTP answer : %s\n", buf);
00317     PRTCL_ERR();
00318   }
00319 
00320   if( (m_httpResponseCode < 200) || (m_httpResponseCode >= 300) )
00321   {
00322     //Did not return a 2xx code; TODO fetch headers/(&data?) anyway and implement a mean of writing/reading headers 
00323     WARN("Response code %d", m_httpResponseCode);
00324     PRTCL_ERR();
00325   }
00326 
00327   DBG("Reading headers");
00328 
00329   memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well
00330   trfLen -= (crlfPos + 2);
00331 
00332   size_t recvContentLength = 0;
00333   bool recvChunked = false;
00334   bool recvLengthUnknown = true;
00335   //Now get headers
00336   while( true )
00337   {
00338     crlfPtr = strstr(buf, "\r\n");
00339     if(crlfPtr == NULL)
00340     {
00341       if( trfLen < CHUNK_SIZE - 1 )
00342       {
00343         size_t newTrfLen;
00344         ret = recv(buf + trfLen, 1, CHUNK_SIZE - trfLen - 1, &newTrfLen);
00345         trfLen += newTrfLen;
00346         buf[trfLen] = '\0';
00347         DBG("Read %d chars; In buf: [%s]", newTrfLen, buf);
00348         CHECK_CONN_ERR(ret);
00349         continue;
00350       }
00351       else
00352       {
00353         PRTCL_ERR();
00354       }
00355     }
00356 
00357     crlfPos = crlfPtr - buf;
00358 
00359     if(crlfPos == 0) //End of headers
00360     {
00361       DBG("Headers read");
00362       memmove(buf, &buf[2], trfLen - 2 + 1); //Be sure to move NULL-terminating char as well
00363       trfLen -= 2;
00364       break;
00365     }
00366 
00367     buf[crlfPos] = '\0';
00368 
00369     char key[KEY_VAL_BUF_SIZE];
00370     char value[KEY_VAL_BUF_SIZE];
00371 
00372     //key[31] = '\0';
00373     //value[31] = '\0';
00374 
00375     memset(key, 0, KEY_VAL_BUF_SIZE);
00376     memset(value, 0, KEY_VAL_BUF_SIZE);
00377 
00378     //int n = sscanf(buf, "%31[^:]: %31[^\r\n]", key, value);
00379     
00380     int n = 0;
00381     
00382     char* keyEnd = strchr(buf, ':');
00383     if(keyEnd != NULL)
00384     {
00385 //DBG("Test01 : %u\n", strlen(buf));      
00386 
00387       *keyEnd = '\0';
00388       
00389 //DBG("Test02 : %u\n", strlen(buf));      
00390       
00391       if(strlen(buf) < KEY_VAL_BUF_SIZE)
00392       {
00393         strcpy(key, buf);
00394         n++;
00395         char* valueStart = keyEnd + 2;
00396         
00397 //DBG("Test03 : %u, %u\n", valueStart - buf, crlfPos);              
00398         if( (valueStart - buf) < crlfPos )
00399         {
00400           if(strlen(valueStart) < KEY_VAL_BUF_SIZE)
00401           { 
00402             strcpy(value, valueStart);
00403             n++;
00404           }
00405         }
00406       }
00407     }
00408     if ( n == 2 )
00409     {
00410       DBG("Read header : %s: %s\n", key, value);
00411       if( !strcmp(key, "Content-Length") )
00412       {
00413         sscanf(value, "%d", &recvContentLength);
00414         recvLengthUnknown = false;
00415         pDataIn->setDataLen(recvContentLength);
00416       }
00417       else if( !strcmp(key, "Transfer-Encoding") )
00418       {
00419         if( !strcmp(value, "Chunked") || !strcmp(value, "chunked") )
00420         {
00421           recvChunked = true;
00422           recvLengthUnknown = false;
00423           pDataIn->setIsChunked(true);
00424         }
00425       }
00426       else if( !strcmp(key, "Content-Type") )
00427       {
00428         pDataIn->setDataType(value);
00429       }
00430 
00431       memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well
00432       trfLen -= (crlfPos + 2);
00433 
00434     }
00435     else
00436     {
00437       ERR("Could not parse header");
00438       PRTCL_ERR();
00439     }
00440 
00441   }
00442 
00443   //Receive data
00444   DBG("Receiving data");
00445   while(true)
00446   {
00447     size_t readLen = 0;
00448 
00449     if( recvChunked )
00450     {
00451       //Read chunk header
00452       bool foundCrlf;
00453       do
00454       {
00455         foundCrlf = false;
00456         crlfPos=0;
00457         buf[trfLen]=0;
00458         if(trfLen >= 2)
00459         {
00460           for(; crlfPos < trfLen - 2; crlfPos++)
00461           {
00462             if( buf[crlfPos] == '\r' && buf[crlfPos + 1] == '\n' )
00463             {
00464               foundCrlf = true;
00465               break;
00466             }
00467           }
00468         }
00469         if(!foundCrlf) //Try to read more
00470         {
00471           if( trfLen < CHUNK_SIZE )
00472           {
00473             size_t newTrfLen;
00474             ret = recv(buf + trfLen, 0, CHUNK_SIZE - trfLen - 1, &newTrfLen);
00475             trfLen += newTrfLen;
00476             CHECK_CONN_ERR(ret);
00477             continue;
00478           }
00479           else
00480           {
00481             PRTCL_ERR();
00482           }
00483         }
00484       } while(!foundCrlf);
00485       buf[crlfPos] = '\0';
00486       int n = sscanf(buf, "%x", &readLen);
00487       if(n!=1)
00488       {
00489         ERR("Could not read chunk length");
00490         PRTCL_ERR();
00491       }
00492 
00493       memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2)); //Not need to move NULL-terminating char any more
00494       trfLen -= (crlfPos + 2);
00495 
00496       if( readLen == 0 )
00497       {
00498         //Last chunk
00499         break;
00500       }
00501     }
00502     else
00503     {
00504       readLen = recvContentLength;
00505     }
00506 
00507     DBG("Retrieving %d bytes (%d bytes in buffer)", readLen, trfLen);
00508 
00509     do
00510     {
00511       if(recvLengthUnknown )
00512       {
00513         readLen = trfLen;
00514       }
00515       pDataIn->write(buf, MIN(trfLen, readLen));
00516       if(!recvLengthUnknown)
00517       {
00518         if( trfLen > readLen )
00519         {
00520           memmove(buf, &buf[readLen], trfLen - readLen);
00521           trfLen -= readLen;
00522           readLen = 0;
00523         }
00524         else
00525         {
00526           readLen -= trfLen;
00527         }
00528       }
00529       else
00530       {
00531         trfLen = 0;
00532       }
00533 
00534       if(readLen || recvLengthUnknown)
00535       {
00536         ret = recv(buf, 1, CHUNK_SIZE - trfLen - 1, &trfLen);
00537         if(recvLengthUnknown && (ret == HTTP_CLOSED))
00538         {
00539           //Write and exit
00540           pDataIn->write(buf, trfLen);
00541           break;
00542         }
00543         CHECK_CONN_ERR(ret);
00544         if(recvLengthUnknown && (trfLen == 0))
00545         {
00546           break;
00547         }
00548       }
00549     } while(readLen || recvLengthUnknown);
00550 
00551     if( recvChunked )
00552     {
00553       if(trfLen < 2)
00554       {
00555         size_t newTrfLen;
00556         //Read missing chars to find end of chunk
00557         ret = recv(buf + trfLen, 2 - trfLen, CHUNK_SIZE - trfLen - 1, &newTrfLen);
00558         CHECK_CONN_ERR(ret);
00559         trfLen += newTrfLen;
00560       }
00561       if( (buf[0] != '\r') || (buf[1] != '\n') )
00562       {
00563         ERR("Format error");
00564         PRTCL_ERR();
00565       }
00566       memmove(buf, &buf[2], trfLen - 2);
00567       trfLen -= 2;
00568     }
00569     else
00570     {
00571       break;
00572     }
00573 
00574   }
00575 
00576   m_sock.close();
00577   DBG("Completed HTTP transaction");
00578 
00579   return HTTP_OK;
00580 }
00581 
00582 HTTPResult HTTPClient::recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen) //0 on success, err code on failure
00583 {
00584   DBG("Trying to read between %d and %d bytes", minLen, maxLen);
00585   size_t readLen = 0;
00586       
00587   if(!m_sock.is_connected())
00588   {
00589     WARN("Connection was closed by server");
00590     return HTTP_CLOSED; //Connection was closed by server 
00591   }
00592     
00593   int ret;
00594   while(readLen < maxLen)
00595   {
00596     if(readLen < minLen)
00597     {
00598       DBG("Trying to read at most %d bytes [Blocking]", minLen - readLen);
00599       m_sock.set_blocking(false, m_timeout);
00600       ret = m_sock.receive_all(buf + readLen, minLen - readLen);
00601     }
00602     else
00603     {
00604       DBG("Trying to read at most %d bytes [Not blocking]", maxLen - readLen);
00605       m_sock.set_blocking(false, 0);
00606       ret = m_sock.receive(buf + readLen, maxLen - readLen);
00607     }
00608     
00609     if( ret > 0)
00610     {
00611       readLen += ret;
00612     }
00613     else if( ret == 0 )
00614     {
00615       break;
00616     }
00617     else
00618     {
00619       if(!m_sock.is_connected())
00620       {
00621         ERR("Connection error (recv returned %d)", ret);
00622         *pReadLen = readLen;
00623         return HTTP_CONN;
00624       }
00625       else
00626       {
00627         break;      
00628       }
00629     }
00630     
00631     if(!m_sock.is_connected())
00632     {
00633       break;
00634     }
00635   }
00636   DBG("Read %d bytes", readLen);
00637   *pReadLen = readLen;
00638   return HTTP_OK;
00639 }
00640 
00641 HTTPResult HTTPClient::send(char* buf, size_t len) //0 on success, err code on failure
00642 {
00643   if(len == 0)
00644   {
00645     len = strlen(buf);
00646   }
00647   DBG("Trying to write %d bytes", len);
00648   size_t writtenLen = 0;
00649     
00650   if(!m_sock.is_connected())
00651   {
00652     WARN("Connection was closed by server");
00653     return HTTP_CLOSED; //Connection was closed by server 
00654   }
00655   
00656   m_sock.set_blocking(false, m_timeout);
00657   int ret = m_sock.send_all(buf, len);
00658   if(ret > 0)
00659   {
00660     writtenLen += ret;
00661   }
00662   else if( ret == 0 )
00663   {
00664     WARN("Connection was closed by server");
00665     return HTTP_CLOSED; //Connection was closed by server
00666   }
00667   else
00668   {
00669     ERR("Connection error (send returned %d)", ret);
00670     return HTTP_CONN;
00671   }
00672   
00673   DBG("Written %d bytes", writtenLen);
00674   return HTTP_OK;
00675 }
00676 
00677 HTTPResult HTTPClient::parseURL(const char* url, char* scheme, size_t maxSchemeLen, char* host, size_t maxHostLen, uint16_t* port, char* path, size_t maxPathLen) //Parse URL
00678 {
00679   char* schemePtr = (char*) url;
00680   char* hostPtr = (char*) strstr(url, "://");
00681   if(hostPtr == NULL)
00682   {
00683     WARN("Could not find host");
00684     return HTTP_PARSE; //URL is invalid
00685   }
00686 
00687   if( maxSchemeLen < hostPtr - schemePtr + 1 ) //including NULL-terminating char
00688   {
00689     WARN("Scheme str is too small (%d >= %d)", maxSchemeLen, hostPtr - schemePtr + 1);
00690     return HTTP_PARSE;
00691   }
00692   memcpy(scheme, schemePtr, hostPtr - schemePtr);
00693   scheme[hostPtr - schemePtr] = '\0';
00694 
00695   hostPtr+=3;
00696 
00697   size_t hostLen = 0;
00698 
00699   char* portPtr = strchr(hostPtr, ':');
00700   if( portPtr != NULL )
00701   {
00702     hostLen = portPtr - hostPtr;
00703     portPtr++;
00704     if( sscanf(portPtr, "%hu", port) != 1)
00705     {
00706       WARN("Could not find port");
00707       return HTTP_PARSE;
00708     }
00709   }
00710   else
00711   {
00712     *port=0;
00713   }
00714   char* pathPtr = strchr(hostPtr, '/');
00715   if( hostLen == 0 )
00716   {
00717     hostLen = pathPtr - hostPtr;
00718   }
00719 
00720   if( maxHostLen < hostLen + 1 ) //including NULL-terminating char
00721   {
00722     WARN("Host str is too small (%d >= %d)", maxHostLen, hostLen + 1);
00723     return HTTP_PARSE;
00724   }
00725   memcpy(host, hostPtr, hostLen);
00726   host[hostLen] = '\0';
00727 
00728   size_t pathLen;
00729   char* fragmentPtr = strchr(hostPtr, '#');
00730   if(fragmentPtr != NULL)
00731   {
00732     pathLen = fragmentPtr - pathPtr;
00733   }
00734   else
00735   {
00736     pathLen = strlen(pathPtr);
00737   }
00738 
00739   if( maxPathLen < pathLen + 1 ) //including NULL-terminating char
00740   {
00741     WARN("Path str is too small (%d >= %d)", maxPathLen, pathLen + 1);
00742     return HTTP_PARSE;
00743   }
00744   memcpy(path, pathPtr, pathLen);
00745   path[pathLen] = '\0';
00746 
00747   return HTTP_OK;
00748 }