3 #include "hphp/runtime/base/file.h"
4 #include "hphp/runtime/base/req-optional.h"
5 #include "hphp/runtime/base/string-buffer.h"
6 #include "hphp/runtime/ext/extension.h"
8 #include "hphp/util/type-scan.h"
10 #include <curl/curl.h>
13 /////////////////////////////////////////////////////////////////////////////
16 struct CurlMultiResource
;
18 struct CurlResource
: SweepableResourceData
{
19 using ExceptionType
= req::Optional
<boost::variant
<Object
,Exception
*>>;
38 std::vector
<char*> str
;
39 std::vector
<curl_httppost
*> post
;
40 std::vector
<curl_slist
*> slist
;
43 using fb_specific_options
= enum {
44 CURLOPT_FB_TLS_VER_MAX
= 2147482624,
45 CURLOPT_FB_TLS_VER_MAX_NONE
= 2147482625,
46 CURLOPT_FB_TLS_VER_MAX_1_1
= 2147482626,
47 CURLOPT_FB_TLS_VER_MAX_1_0
= 2147482627,
48 CURLOPT_FB_TLS_CIPHER_SPEC
= 2147482628
52 const String
& o_getClassNameHook() const override
{ return classnameof(); }
53 DECLARE_RESOURCE_ALLOCATION(CurlResource
)
54 bool isInvalid() const override
{ return !m_cp
; }
56 explicit CurlResource(const String
& url
);
57 ~CurlResource() { close(); }
65 String
getUrl() { return m_url
; }
66 String
getHeader() { return m_header
; }
69 bool setOption(long option
, const Variant
& value
);
70 Variant
getOption(long option
);
72 int getError() { return m_error_no
; }
73 String
getErrorString() { return String(m_error_str
, CopyString
); }
77 void check_exception();
78 ExceptionType
getAndClearException() { return std::move(m_exception
); }
79 static bool isPhpException(const ExceptionType
& e
) {
80 return e
&& boost::get
<Object
>(&e
.value()) != nullptr;
82 static Object
getPhpException(const ExceptionType
& e
) {
83 assertx(e
&& isPhpException(e
));
84 return boost::get
<Object
>(*e
);
86 static Exception
* getCppException(const ExceptionType
& e
) {
87 assertx(e
&& !isPhpException(e
));
88 return boost::get
<Exception
*>(*e
);
92 void setDefaultOptions();
94 static int64_t minTimeout(int64_t timeout
);
95 static int64_t minTimeoutMS(int64_t timeout
);
97 static bool isLongOption(long option
);
98 bool setLongOption(long option
, long value
);
99 static bool isStringOption(long option
);
100 static bool isStringFilePathOption(long option
);
101 bool setStringOption(long option
, const String
& value
);
102 static bool isNullableStringOption(long option
);
103 bool setNullableStringOption(long option
, const Variant
& value
);
104 bool setPostFieldsOption(const Variant
& value
);
105 static bool isFileOption(long option
);
106 bool setFileOption(long option
, const req::ptr
<File
>& fp
);
107 static bool isStringListOption(long option
);
108 bool setStringListOption(long option
, const Variant
& value
);
109 static bool isNonCurlOption(long option
);
110 bool setNonCurlOption(long option
, const Variant
& value
);
111 static bool isBlobOption(long option
);
112 bool setBlobOption(long option
, const String
& value
);
114 void handle_exception();
115 static size_t curl_read(char *data
, size_t size
, size_t nmemb
, void *ctx
);
116 static size_t curl_write(char *data
, size_t size
, size_t nmemb
, void *ctx
);
117 static size_t curl_write_header(char *data
,
118 size_t size
, size_t nmemb
, void *ctx
);
119 static int curl_debug(CURL
*cp
, curl_infotype type
, char *buf
,
120 size_t buf_len
, void *ctx
);
121 static int curl_progress(void* p
,
122 double dltotal
, double dlnow
,
123 double ultotal
, double ulnow
);
125 static CURLcode
ssl_ctx_callback(CURL
*curl
, void *sslctx
, void *parm
);
128 friend struct CurlMultiResource
;
131 TYPE_SCAN_IGNORE_FIELD(m_cp
);
132 CurlMultiResource
* m_multi
;
133 TYPE_SCAN_IGNORE_FIELD(m_multi
);
134 ExceptionType m_exception
;
136 char m_error_str
[CURL_ERROR_SIZE
+ 1];
139 req::shared_ptr
<ToFree
> m_to_free
;
145 WriteHandler m_write
;
146 WriteHandler m_write_header
;
148 Variant m_progress_callback
;
150 bool m_in_callback
{false};
151 bool m_in_exec
{false};
156 /////////////////////////////////////////////////////////////////////////////