Remove easy handles before curl_multi_cleanup
[hiphop-php.git] / hphp / runtime / ext / curl / curl-resource.h
blob8ee2e76d48cb8ae10c4b6b71d96bdcad2dd99cd8
1 #pragma once
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>
12 namespace HPHP {
13 /////////////////////////////////////////////////////////////////////////////
14 // CurlResource
16 struct CurlMultiResource;
18 struct CurlResource : SweepableResourceData {
19 using ExceptionType = req::Optional<boost::variant<Object,Exception*>>;
21 struct WriteHandler {
22 int method{0};
23 Variant callback;
24 req::ptr<File> fp;
25 StringBuffer buf;
26 String content;
27 int type{0};
30 struct ReadHandler {
31 int method{0};
32 Variant callback;
33 req::ptr<File> fp;
36 struct ToFree {
37 ~ToFree();
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
51 CLASSNAME_IS("curl")
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(); }
59 void closeForSweep();
60 void close();
61 void reseat();
62 void reset();
64 Variant execute();
65 String getUrl() { return m_url; }
66 String getHeader() { return m_header; }
67 String getContents();
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); }
75 CURL *get();
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);
91 private:
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);
127 private:
128 friend struct CurlMultiResource;
130 CURL *m_cp;
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];
137 CURLcode m_error_no;
139 req::shared_ptr<ToFree> m_to_free;
141 String m_url;
142 String m_header;
143 Array m_opts;
145 WriteHandler m_write;
146 WriteHandler m_write_header;
147 ReadHandler m_read;
148 Variant m_progress_callback;
150 bool m_in_callback{false};
151 bool m_in_exec{false};
152 bool m_emptyPost;
153 bool m_safeUpload;
156 /////////////////////////////////////////////////////////////////////////////