This is older repository, see HTTPClientForFIAP. 新しい Version のリポジトリーが HTTPClientForFIAP にあります。

Fork of HTTPClientForSOAP by Yasushi TAUCHI

Revision:
14:477c357c1c24
Parent:
13:be61104f4e91
Child:
15:e09afb9ca1cd
--- a/HTTPClient.cpp	Sun Aug 05 16:12:10 2012 +0000
+++ b/HTTPClient.cpp	Mon Aug 20 09:50:29 2012 +0000
@@ -81,6 +81,10 @@
 {
   return connect(url, HTTP_POST, (IHTTPDataOut*)&dataOut, pDataIn, timeout);
 }
+HTTPResult HTTPClient::postXML(const char* url,const char* SOAPAction, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
+{
+  return connect(url, HTTP_POST, SOAPAction, (IHTTPDataOut*)&dataOut, pDataIn, timeout);
+}
 
 int HTTPClient::getHTTPResponseCode()
 {
@@ -454,6 +458,364 @@
   return HTTP_OK;
 }
 
+HTTPResult HTTPClient::connect(const char* url, HTTP_METH method, const char* SOAPAction, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout) //Execute request
+{ 
+  m_httpResponseCode = 0; //Invalidate code
+  m_timeout = timeout;
+
+  char scheme[8];
+  uint16_t port;
+  char host[32];
+  char path[64];
+  //First we need to parse the url (http[s]://host[:port][/[path]]) -- HTTPS not supported (yet?)
+  HTTPResult res = parseURL(url, scheme, sizeof(scheme), host, sizeof(host), &port, path, sizeof(path));
+  if(res != HTTP_OK)
+  {
+    ERR("parseURL returned %d", res);
+    return res;
+  }
+
+  if(port == 0) //TODO do handle HTTPS->443
+  {
+    port = 80;
+  }
+
+  DBG("Scheme: %s", scheme);
+  DBG("Host: %s", host);
+  DBG("Port: %d", port);
+  DBG("Path: %s", path);
+
+  //Connect
+  DBG("Connecting socket to server");
+  int ret = m_sock.connect(host, port);
+  if (ret < 0)
+  {
+    m_sock.close();
+    ERR("Could not connect");
+    return HTTP_CONN;
+  }
+
+  //Send request
+  DBG("Sending request");
+  char buf[CHUNK_SIZE];
+  const char* meth = (method==HTTP_GET)?"GET":(method==HTTP_POST)?"POST":"";
+  snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s\r\n", meth, path, host); //Write request
+  ret = send(buf);
+  if(ret)
+  {
+    m_sock.close();
+    ERR("Could not write request");
+    return HTTP_CONN;
+  }
+
+  //Send all headers
+
+  //Send default headers
+  DBG("Sending headers");
+  if( (method == HTTP_POST) && (pDataOut != NULL) )
+  {
+    if( pDataOut->getIsChunked() )
+    {
+      ret = send("Transfer-Encoding: chunked\r\n");
+      CHECK_CONN_ERR(ret);
+    }
+    else
+    {
+      snprintf(buf, sizeof(buf), "Content-Length: %d\r\n", pDataOut->getDataLen());
+      ret = send(buf);
+      CHECK_CONN_ERR(ret);
+    }
+    char type[48];
+    if( pDataOut->getDataType(type, 48) == HTTP_OK )
+    {
+      snprintf(buf, sizeof(buf), "Content-Type: text/xml;charset=UTF-8\r\n");
+      ret = send(buf);
+      CHECK_CONN_ERR(ret);
+      snprintf(buf, sizeof(buf), "SOAPAction: \"%s\"\r\n", type);
+      ret = send(buf);
+      CHECK_CONN_ERR(ret);
+//      snprintf(buf, sizeof(buf), "Content-Type: %s\r\n", type);
+//      ret = send(buf);
+//      CHECK_CONN_ERR(ret);
+    }
+  }
+  
+  //Close headers
+  DBG("Headers sent");
+  ret = send("\r\n");
+  CHECK_CONN_ERR(ret);
+
+  size_t trfLen;
+  
+  //Send data (if POST)
+  if( (method == HTTP_POST) && (pDataOut != NULL) )
+  {
+    DBG("Sending data");
+    while(true)
+    {
+      size_t writtenLen = 0;
+      pDataOut->read(buf, CHUNK_SIZE, &trfLen);
+      if( pDataOut->getIsChunked() )
+      {
+        //Write chunk header
+        char chunkHeader[16];
+        snprintf(chunkHeader, sizeof(chunkHeader), "%X\r\n", trfLen); //In hex encoding
+        ret = send(chunkHeader);
+        CHECK_CONN_ERR(ret);
+      }
+      else if( trfLen == 0 )
+      {
+        break;
+      }
+      if( trfLen != 0 )
+      {
+        ret = send(buf, trfLen);
+        CHECK_CONN_ERR(ret);
+      }
+
+      if( pDataOut->getIsChunked()  )
+      {
+        ret = send("\r\n"); //Chunk-terminating CRLF
+        CHECK_CONN_ERR(ret);
+      }
+      else
+      {
+        writtenLen += trfLen;
+        if( writtenLen >= pDataOut->getDataLen() )
+        {
+          break;
+        }
+      }
+
+      if( trfLen == 0 )
+      {
+        break;
+      }
+    }
+
+  }
+  
+  //Receive response
+  DBG("Receiving response");
+  ret = recv(buf, CHUNK_SIZE - 1, CHUNK_SIZE - 1, &trfLen); //Read n bytes
+  CHECK_CONN_ERR(ret);
+
+  buf[trfLen] = '\0';
+
+  char* crlfPtr = strstr(buf, "\r\n");
+  if(crlfPtr == NULL)
+  {
+    PRTCL_ERR();
+  }
+
+  int crlfPos = crlfPtr - buf;
+  buf[crlfPos] = '\0';
+
+  //Parse HTTP response
+  if( sscanf(buf, "HTTP/%*d.%*d %d %*[^\r\n]", &m_httpResponseCode) != 1 )
+  {
+    //Cannot match string, error
+    ERR("Not a correct HTTP answer : %s\n", buf);
+    PRTCL_ERR();
+  }
+
+  if(m_httpResponseCode != 200)
+  {
+    //Cannot match string, error
+    WARN("Response code %d", m_httpResponseCode);
+    PRTCL_ERR();
+  }
+
+  DBG("Reading headers");
+
+  memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well
+  trfLen -= (crlfPos + 2);
+
+  size_t recvContentLength = 0;
+  bool recvChunked = false;
+  //Now get headers
+  while( true )
+  {
+    crlfPtr = strstr(buf, "\r\n");
+    if(crlfPtr == NULL)
+    {
+      if( trfLen < CHUNK_SIZE - 1 )
+      {
+        size_t newTrfLen;
+        ret = recv(buf + trfLen, 1, CHUNK_SIZE - trfLen - 1, &newTrfLen);
+        trfLen += newTrfLen;
+        buf[trfLen] = '\0';
+        DBG("Read %d chars; In buf: [%s]", newTrfLen, buf);
+        CHECK_CONN_ERR(ret);
+        continue;
+      }
+      else
+      {
+        PRTCL_ERR();
+      }
+    }
+
+    crlfPos = crlfPtr - buf;
+
+    if(crlfPos == 0) //End of headers
+    {
+      DBG("Headers read");
+      memmove(buf, &buf[2], trfLen - 2 + 1); //Be sure to move NULL-terminating char as well
+      trfLen -= 2;
+      break;
+    }
+
+    buf[crlfPos] = '\0';
+
+    char key[32];
+    char value[32];
+
+    key[31] = '\0';
+    value[31] = '\0';
+
+    int n = sscanf(buf, "%31[^:]: %31[^\r\n]", key, value);
+    if ( n == 2 )
+    {
+      DBG("Read header : %s: %s\n", key, value);
+      if( !strcmp(key, "Content-Length") )
+      {
+        sscanf(value, "%d", &recvContentLength);
+        pDataIn->setDataLen(recvContentLength);
+      }
+      else if( !strcmp(key, "Transfer-Encoding") )
+      {
+        if( !strcmp(value, "Chunked") || !strcmp(value, "chunked") )
+        {
+          recvChunked = true;
+          pDataIn->setIsChunked(true);
+        }
+      }
+      else if( !strcmp(key, "Content-Type") )
+      {
+        pDataIn->setDataType(value);
+      }
+
+      memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well
+      trfLen -= (crlfPos + 2);
+
+    }
+    else
+    {
+      ERR("Could not parse header");
+      PRTCL_ERR();
+    }
+
+  }
+
+  //Receive data
+  DBG("Receiving data");
+  while(true)
+  {
+    size_t readLen = 0;
+
+    if( recvChunked )
+    {
+      //Read chunk header
+      crlfPos=0;
+      for(crlfPos++; crlfPos < trfLen - 2; crlfPos++)
+      {
+        if( buf[crlfPos] == '\r' && buf[crlfPos + 1] == '\n' )
+        {
+          break;
+        }
+      }
+      if(crlfPos >= trfLen - 2) //Try to read more
+      {
+        if( trfLen < CHUNK_SIZE )
+        {
+          size_t newTrfLen;
+          ret = recv(buf + trfLen, 0, CHUNK_SIZE - trfLen - 1, &newTrfLen);
+          trfLen += newTrfLen;
+          CHECK_CONN_ERR(ret);
+          continue;
+        }
+        else
+        {
+          PRTCL_ERR();
+        }
+      }
+      buf[crlfPos] = '\0';
+      int n = sscanf(buf, "%x", &readLen);
+      if(n!=1)
+      {
+        ERR("Could not read chunk length");
+        PRTCL_ERR();
+      }
+
+      memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2)); //Not need to move NULL-terminating char any more
+      trfLen -= (crlfPos + 2);
+
+      if( readLen == 0 )
+      {
+        //Last chunk
+        break;
+      }
+    }
+    else
+    {
+      readLen = recvContentLength;
+    }
+
+    DBG("Retrieving %d bytes", readLen);
+
+    do
+    {
+      pDataIn->write(buf, MIN(trfLen, readLen));
+      if( trfLen > readLen )
+      {
+        memmove(buf, &buf[readLen], trfLen - readLen);
+        trfLen -= readLen;
+        readLen = 0;
+      }
+      else
+      {
+        readLen -= trfLen;
+      }
+
+      if(readLen)
+      {
+        ret = recv(buf, 1, CHUNK_SIZE - trfLen - 1, &trfLen);
+        CHECK_CONN_ERR(ret);
+      }
+    } while(readLen);
+
+    if( recvChunked )
+    {
+      if(trfLen < 2)
+      {
+        size_t newTrfLen;
+        //Read missing chars to find end of chunk
+        ret = recv(buf, 2 - trfLen, CHUNK_SIZE, &newTrfLen);
+        CHECK_CONN_ERR(ret);
+        trfLen += newTrfLen;
+      }
+      if( (buf[0] != '\r') || (buf[1] != '\n') )
+      {
+        ERR("Format error");
+        PRTCL_ERR();
+      }
+      memmove(buf, &buf[2], trfLen - 2);
+      trfLen -= 2;
+    }
+    else
+    {
+      break;
+    }
+
+  }
+
+  m_sock.close();
+  DBG("Completed HTTP transaction");
+
+  return HTTP_OK;
+}
+
+
 HTTPResult HTTPClient::recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen) //0 on success, err code on failure
 {
   DBG("Trying to read between %d and %d bytes", minLen, maxLen);