codemod 2010-2016 to 2010-present
[hiphop-php.git] / hphp / runtime / base / http-client.h
blob6e09b8291e83721bd374e12bb3d1f2e803789339
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_HTTP_CLIENT_H_
18 #define incl_HPHP_HTTP_CLIENT_H_
20 #include <vector>
22 #include "hphp/runtime/base/string-buffer.h"
23 #include "hphp/runtime/base/type-array.h"
24 #include "hphp/runtime/server/transport.h"
25 #include <curl/curl.h>
27 namespace HPHP {
28 ///////////////////////////////////////////////////////////////////////////////
30 struct HttpClient {
31 explicit HttpClient(int timeout = 5 /* seconds */, int maxRedirect = 1,
32 bool use11 = true, bool decompress = false);
34 /**
35 * Authentication.
37 void auth(const std::string &username, const std::string &password,
38 bool basic = true);
39 /**
40 * Set up proxy server.
42 void proxy(const std::string &host, int port,
43 const std::string &username = "",
44 const std::string &password = "");
46 /**
47 * StreamContext settings for this connection
49 void setStreamContextOptions(const Array &opts) {
50 m_stream_context_options = opts;
52 /**
53 * require SLS/TLS
54 * (default) CURLUSESSL_NONE, CURLUSESSL_TRY, CURLUSESSL_CONTROL,
55 * CURLUSESSL_ALL
58 void setUseSSL(const long level) {
59 m_use_ssl = level;
62 /**
63 * set preferred TLS/SSL version
64 * (default) CURL_SSLVERSION_DEFAULT, CURL_SSLVERSION_TLSv1,
65 * CURL_SSLVERSION_SSLv3
67 void setSSLVersion (const long version) {
68 m_sslversion = version;
71 /**
72 * GET an URL and returns its response code.
74 int get(const char *url, StringBuffer &response,
75 const HeaderMap *requestHeaders = nullptr,
76 std::vector<String> *responseHeaders = nullptr);
78 /**
79 * POST data to an URL and returns its response code.
81 int post(const char *url, const char *data, size_t size,
82 StringBuffer &response,
83 const HeaderMap *requestHeaders = nullptr,
84 std::vector<String> *responseHeaders = nullptr);
86 int request(const char* method,
87 const char *url, const char *data, size_t size,
88 StringBuffer &response, const HeaderMap *requestHeaders,
89 std::vector<String> *responseHeaders);
91 const std::string& getLastError() const {
92 return m_error;
95 static const int defaultMaxRedirect = 20;
97 private:
98 int m_timeout;
99 int m_maxRedirect;
100 bool m_use11;
101 bool m_decompress;
103 StringBuffer *m_response;
104 std::vector<String> *m_responseHeaders;
105 std::string m_error;
107 bool m_basic;
108 std::string m_username;
109 std::string m_password;
111 std::string m_proxyHost;
112 int m_proxyPort;
113 std::string m_proxyUsername;
114 std::string m_proxyPassword;
116 long m_use_ssl = CURLUSESSL_NONE;
117 long m_sslversion = CURL_SSLVERSION_DEFAULT; //try to match remote SSL
119 Array m_stream_context_options;
121 static size_t curl_write(char *data, size_t size, size_t nmemb, void *ctx);
122 static size_t curl_header(char *data, size_t size, size_t nmemb, void *ctx);
124 size_t write(char *data, size_t size, size_t nmemb);
125 size_t header(char *data, size_t size, size_t nmemb);
128 ///////////////////////////////////////////////////////////////////////////////
131 #endif // incl_HPHP_HTTP_CLIENT_H_