当前位置: 首页>>代码示例>>C++>>正文


C++ HttpHeaders::appendHeaderString方法代码示例

本文整理汇总了C++中HttpHeaders::appendHeaderString方法的典型用法代码示例。如果您正苦于以下问题:C++ HttpHeaders::appendHeaderString方法的具体用法?C++ HttpHeaders::appendHeaderString怎么用?C++ HttpHeaders::appendHeaderString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HttpHeaders的用法示例。


在下文中一共展示了HttpHeaders::appendHeaderString方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: headers_callback

  /*
   * This function serves as a callback for response headers read by libcurl
   *
   * The libcurl documentation at
   * http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTHEADERFUNCTION
   * says that the callback is called once for each header and only complete
   * header line are passed. So we do not need to handle multi-line headers
   * ourselves.
   *
   * Please see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html
   *   The first response header returned will always be "Status-line",
   * Also see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
   */
  static size_t headers_callback(void *buffer, size_t size, size_t nmemb, void *userp) {
    char *buf = reinterpret_cast<char*>(buffer);
    HttpHeaders *pHeaders = reinterpret_cast<HttpHeaders*>(userp);
    size_t result = 0u;
    if (pHeaders != NULL) {
      /*
       * Note: std::string is capable of storing binary stream data and can
       * store "\0" as normal character (witout terminating string).
       *
       * - Until C++11, it is not guaranteed to be contiguously stored though.
       * - Use caution when using .c_str(), if string contains "\0", use .data() instead
       *
       * Since binary data can be stored in std::string, unicode characters
       * can be present But beware of using .length() or .size(), since they
       * will return number of bytes storage (i.e., char) needed to store the
       * string, and not actual number of characters.
       */
      std::string s = "";
      s.append(buf, size * nmemb);
      result = size * nmemb;

      // There can be 3 different cases, each should be handled differently:

      // Case 1: Check for last header line: CRLF,
      //         http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html
      if (s.size() == 2u && s[0] == '\r' && s[1] == '\n')
        return result;

      // Case 2: If it is the first header, the it must be a status Line.
      // For all other cases, statusLine must be populated with some non-zero length string
      if (pHeaders->getStatusLine().length() == 0u) {
        pHeaders->setStatusLine(HttpHelperUtils::stripWhitespaces(s));
        return result;
      }

      // Case 3: This is a usual message header, of form
      // message-header = field-name ":" [ field-value ]
      // http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
      pHeaders->appendHeaderString(s);
    }

    return result;
  }
开发者ID:ivoloshanenko,项目名称:dx-toolkit,代码行数:56,代码来源:SimpleHttp.cpp


注:本文中的HttpHeaders::appendHeaderString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。