示例#1
0
int main(int argc, char* argv[])
{
	CHttpResponse r;

	string str;
	char* buff = strdup("HTTP/1.1 500 ( 无效索引。  )\r\nVia: 1.1 SERVER\r\nConnection: close\r\nProxy-Connection: close\r\nPragma: no-cache\r\nCache-Control: no-cache\r\nContent-Type: text/html\r\nContent-Length: 4\r\n");
	r.Parse(buff, strlen(buff));
	assert(r.GetCode() == 500);
	assert(r.GetHeader("Via", str) && str == "1.1 SERVER");
	assert(r.GetHeader("Content-Length", str) && str == "4");
	assert(r.SetBody((char*)strdup("1234"), 4));
	assert(strncmp((char*)r.GetBodyData(), "1234", r.GetBodyLength()) == 0);
	free(buff);

	return 0;
}
示例#2
0
static int OnHttpRequest(void* cls,
                         struct MHD_Connection* connection,
                         const char* url,
                         const char* method,
                         const char* version,
                         const char* upload_data,
                         size_t* upload_data_size,
                         void** ptr)
{
  if (!*ptr)
  {
    *ptr = new std::string;
    return MHD_YES;
  }

  std::string* str = (std::string*) *ptr;

  if (strcmp(method, "POST") == 0 && *upload_data_size)
  {
    *upload_data_size = 0;
    (*str) += upload_data;
    return MHD_YES;
  }

  CLog::Log(LOGDEBUG,"OnHttpRequest - enter function [url=%s][method=%s][version=%s][upload_data=%s][upload_data_size=%lu] (hsrv)",url,method,version,upload_data,*upload_data_size);

  CHttpServer* httpServer = (CHttpServer*)cls;
  if (!httpServer)
  {
    CLog::Log(LOGERROR,"OnHttpRequest - FAILED to get server context (hsrv)");
    return MHD_NO;
  }

  MAPHTTPHEADER header;
  MHD_get_connection_values(connection, MHD_HEADER_KIND, HeadersIterator, &header);

#if 0
  std::map<char*, char*>::const_iterator end = headerParams.end();
  for (std::map<char*, char*>::const_iterator it = headerParams.begin(); it != end; ++it)
  {
      std::cout << "Key: " << it->first;
      std::cout << "Value: " << it->second << '\n';
  }
#endif

  CHttpRequest httpRequest((char*) method, (char*) version, (char*) url, header, str->size(), (char*) str->c_str());
  CHttpResponse httpResponse;

  CLog::Log(LOGDEBUG,"OnHttpRequest - BEFORE HandleRequest with [method=%s][version=%s][url=%s][str=%s][strSize=%lu] (hsrv)",method,version,url,str->c_str(),str->size());
  httpServer->HandleRequest(httpRequest, httpResponse);
  CLog::Log(LOGDEBUG,"OnHttpRequest - AFTER HandleRequest with [method=%s][version=%s][url=%s][str=%s][strSize=%lu]. [IsChunked=%d] (hsrv)",method,version,url,str->c_str(),str->size(),httpResponse.IsChunked());

  struct MHD_Response* response;

  if (httpResponse.IsChunked())
  {
    CStdString deviceId = "";
    IMAPHTTPHEADER it = header.find("X-Boxee-Device-ID");
    if (it == header.end())
    {
      CLog::Log(LOGWARNING,"OnHttpRequest - FAILED to get X-Boxee-Device-ID from header for chunked request (hsrv)");
    }
    else
    {
      deviceId = it->second;
    }

    CReaderCallback* readerCls = new CReaderCallback(deviceId, CStdString(url));
    readerCls->chunkedCB = httpResponse.GetChunkedCallback();

    CLog::Log(LOGDEBUG,"OnHttpRequest - going to response_from_callback. [method=%s][version=%s][url=%s][str=%s][strSize=%lu]. [IsChunked=%d] (hsrv)",method,version,url,str->c_str(),str->size(),httpResponse.IsChunked());

    response = MHD_create_response_from_callback(MHD_SIZE_UNKNOWN, 65535, readerCallback, readerCls, NULL);

    if (MHD_add_response_header(response, "Transfer-Encoding", "chunked") == MHD_NO)
    {
      CLog::Log(LOGERROR,"OnHttpRequest - FAILED to add server to header");
      return MHD_NO;
    }
  }
  else
  {
    unsigned char* bodyPtr = &(httpResponse.GetBody()[0]);
    response = MHD_create_response_from_buffer(httpResponse.GetBody().size(), bodyPtr, MHD_RESPMEM_MUST_COPY);
  }

  if (!response)
  {
    CLog::Log(LOGERROR,"OnHttpRequest - FAILED to create response. [%s] (hsrv)",method);
    return MHD_NO;
  }

  if (MHD_add_response_header(response, "Server", httpServer->GetServerAgent().c_str()) == MHD_NO)
  {
    CLog::Log(LOGERROR,"OnHttpRequest - FAILED to add server to header");
    return MHD_NO;
  }

  std::map<std::string, std::string>::iterator it = httpResponse.GetHeader().begin();
  while (it != httpResponse.GetHeader().end())
  {
    if (MHD_add_response_header(response, it->first.c_str(), it->second.c_str()) == MHD_NO)
    {
      CLog::Log(LOGERROR,"OnHttpRequest - FAILED to add response header [%s=%s] (hsrv)", it->first.c_str(), it->second.c_str());
    }

    it++;
  }

  CLog::Log(LOGDEBUG,"OnHttpRequest - going to queue response. [code=%d][bodySize=%lu] (hsrv)",httpResponse.GetCode(), httpResponse.GetBody().size());

  int retVal = MHD_YES;
  if (MHD_queue_response(connection, httpResponse.GetCode(), response) != MHD_YES)
  {
    CLog::Log(LOGERROR,"OnHttpRequest - FAILED to queue response. [%s] (hsrv)",method);
    retVal = MHD_NO;
  }

  MHD_destroy_response(response);

  delete str;

  return retVal;
}