Skip to content

Commit f28770b

Browse files
authored
Merge pull request #80 from puzza007/curlopt-share
CURL_LOCK_DATA_SSL_SESSION
2 parents 7d44f3a + 0d6b8d8 commit f28770b

File tree

4 files changed

+135
-83
lines changed

4 files changed

+135
-83
lines changed

README.md

+19-18
Original file line numberDiff line numberDiff line change
@@ -102,24 +102,25 @@ katipo:Method(Pool :: atom(), URL :: binary(), ReqOptions :: map()).
102102

103103
#### Request options
104104

105-
| Option | Type | Default | Notes |
106-
|:--------------------|:------------------------------|:------------|:------------------------------------------------------------------------------------|
107-
| `headers` | `[{binary(), iodata()}]` | `[]` | |
108-
| `cookiejar` | opaque (returned in response) | `[]` | |
109-
| `body` | `iodata()` | `<<>>` | |
110-
| `connecttimeout_ms` | `pos_integer()` | 30000 | [docs](https://curl.haxx.se/libcurl/c/CURLOPT_CONNECTTIMEOUT.html) |
111-
| `followlocation` | `boolean()` | `false` | [docs](https://curl.haxx.se/libcurl/c/CURLOPT_FOLLOWLOCATION.html) |
112-
| `ssl_verifyhost` | `boolean()` | `true` | [docs](https://curl.haxx.se/libcurl/c/CURLOPT_SSL_VERIFYHOST.html) |
113-
| `ssl_verifypeer` | `boolean()` | `true` | [docs](https://curl.haxx.se/libcurl/c/CURLOPT_SSL_VERIFYPEER.html) |
114-
| `capath` | `binary()` | `undefined` | |
115-
| `cacert` | `binary()` | `undefined` | |
116-
| `timeout_ms` | `pos_integer()` | 30000 | |
117-
| `maxredirs` | `non_neg_integer()` | 9 | |
118-
| `proxy` | `binary()` | `undefined` | [docs](https://curl.haxx.se/libcurl/c/CURLOPT_PROXY.html) |
119-
| `return_metrics` | `boolean()` | `false` | |
120-
| `tcp_fastopen` | `boolean()` | `false` | [docs](https://curl.haxx.se/libcurl/c/CURLOPT_TCP_FASTOPEN.html) curl >= 7.49.0 |
121-
| `interface` | `binary()` | `undefined` | [docs](https://curl.haxx.se/libcurl/c/CURLOPT_INTERFACE.html) |
122-
| `unix_socket_path` | `binary()` | `undefined` | [docs](https://curl.haxx.se/libcurl/c/CURLOPT_UNIX_SOCKET_PATH.html) curl >= 7.40.0 |
105+
| Option | Type | Default | Notes |
106+
|:------------------------|:------------------------------|:------------|:------------------------------------------------------------------------------------|
107+
| `headers` | `[{binary(), iodata()}]` | `[]` | |
108+
| `cookiejar` | opaque (returned in response) | `[]` | |
109+
| `body` | `iodata()` | `<<>>` | |
110+
| `connecttimeout_ms` | `pos_integer()` | 30000 | [docs](https://curl.haxx.se/libcurl/c/CURLOPT_CONNECTTIMEOUT.html) |
111+
| `followlocation` | `boolean()` | `false` | [docs](https://curl.haxx.se/libcurl/c/CURLOPT_FOLLOWLOCATION.html) |
112+
| `ssl_verifyhost` | `boolean()` | `true` | [docs](https://curl.haxx.se/libcurl/c/CURLOPT_SSL_VERIFYHOST.html) |
113+
| `ssl_verifypeer` | `boolean()` | `true` | [docs](https://curl.haxx.se/libcurl/c/CURLOPT_SSL_VERIFYPEER.html) |
114+
| `capath` | `binary()` | `undefined` | |
115+
| `cacert` | `binary()` | `undefined` | |
116+
| `timeout_ms` | `pos_integer()` | 30000 | |
117+
| `maxredirs` | `non_neg_integer()` | 9 | |
118+
| `proxy` | `binary()` | `undefined` | [docs](https://curl.haxx.se/libcurl/c/CURLOPT_PROXY.html) |
119+
| `return_metrics` | `boolean()` | `false` | |
120+
| `tcp_fastopen` | `boolean()` | `false` | [docs](https://curl.haxx.se/libcurl/c/CURLOPT_TCP_FASTOPEN.html) curl >= 7.49.0 |
121+
| `interface` | `binary()` | `undefined` | [docs](https://curl.haxx.se/libcurl/c/CURLOPT_INTERFACE.html) |
122+
| `unix_socket_path` | `binary()` | `undefined` | [docs](https://curl.haxx.se/libcurl/c/CURLOPT_UNIX_SOCKET_PATH.html) curl >= 7.40.0 |
123+
| `lock_data_ssl_session` | `boolean()` | `false` | [docs](https://curl.haxx.se/libcurl/c/curl_share_setopt.html) curl >= 7.23.0 |
123124

124125
#### Responses
125126

c_src/katipo.c

+17
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#define K_CURLOPT_TCP_FASTOPEN 17
3838
#define K_CURLOPT_INTERFACE 18
3939
#define K_CURLOPT_UNIX_SOCKET_PATH 19
40+
#define K_CURLOPT_LOCK_DATA_SSL_SESSION 20
4041

4142
#define K_CURLAUTH_BASIC 100
4243
#define K_CURLAUTH_DIGEST 101
@@ -49,6 +50,7 @@ typedef struct _GlobalInfo {
4950
struct event_base *evbase;
5051
struct event *timer_event;
5152
CURLM *multi;
53+
CURLSH *shobject;
5254
int still_running;
5355
size_t to_get;
5456
} GlobalInfo;
@@ -105,6 +107,7 @@ typedef struct _EasyOpts {
105107
long curlopt_tcp_fastopen;
106108
char *curlopt_interface;
107109
char *curlopt_unix_socket_path;
110+
long curlopt_lock_data_ssl_session;
108111
} EasyOpts;
109112

110113
static const char *curl_error_code(CURLcode error) {
@@ -802,6 +805,9 @@ static void new_conn(long method, char *url, struct curl_slist *req_headers,
802805
curl_easy_setopt(conn->easy, CURLOPT_COOKIELIST, nc->data);
803806
nc = nc->next;
804807
}
808+
if (eopts.curlopt_lock_data_ssl_session) {
809+
curl_easy_setopt(conn->easy, CURLOPT_SHARE, global->shobject);
810+
}
805811

806812
if (eopts.curlopt_capath != NULL) {
807813
free(eopts.curlopt_capath);
@@ -973,6 +979,7 @@ static void erl_input(struct bufferevent *ev, void *arg) {
973979
eopts.curlopt_tcp_fastopen = 0;
974980
eopts.curlopt_interface = NULL;
975981
eopts.curlopt_unix_socket_path = NULL;
982+
eopts.curlopt_lock_data_ssl_session = 0;
976983

977984
if (ei_decode_list_header(buf, &index, &num_eopts)) {
978985
errx(2, "Couldn't decode eopts length");
@@ -1023,6 +1030,9 @@ static void erl_input(struct bufferevent *ev, void *arg) {
10231030
errx(2, "Unknown curlopt_http_auth value %ld", eopt_long);
10241031
}
10251032
break;
1033+
case K_CURLOPT_LOCK_DATA_SSL_SESSION:
1034+
eopts.curlopt_lock_data_ssl_session = eopt_long;
1035+
break;
10261036
default:
10271037
errx(2, "Unknown eopt long value %ld", eopt);
10281038
}
@@ -1125,6 +1135,13 @@ int main(int argc, char **argv) {
11251135
if (!global.multi) {
11261136
errx(2, "curl_multi_init failed");
11271137
}
1138+
global.shobject = curl_share_init();
1139+
if (!global.shobject) {
1140+
errx(2, "curl_share_init failed");
1141+
}
1142+
if (CURLSHE_OK != curl_share_setopt(global.shobject, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION)) {
1143+
errx(2, "curl_share_setopt failed");
1144+
}
11281145
global.timer_event = evtimer_new(global.evbase, timer_cb, &global);
11291146
global.to_get = 0;
11301147

src/katipo.erl

+14-3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
-define(tcp_fastopen, 17).
6161
-define(interface, 18).
6262
-define(unix_socket_path, 19).
63+
-define(lock_data_ssl_session, 20).
6364

6465
-define(DEFAULT_REQ_TIMEOUT, 30000).
6566
-define(FOLLOWLOCATION_TRUE, 1).
@@ -73,6 +74,8 @@
7374
-define(CURLAUTH_UNDEFINED, 102).
7475
-define(TCP_FASTOPEN_FALSE, 0).
7576
-define(TCP_FASTOPEN_TRUE, 1).
77+
-define(LOCK_DATA_SSL_SESSION_FALSE, 0).
78+
-define(LOCK_DATA_SSL_SESSION_TRUE, 1).
7679

7780
-define(METHODS, [get, post, put, head, options, patch, delete]).
7881

@@ -259,7 +262,9 @@
259262
return_metrics = false :: boolean(),
260263
tcp_fastopen = ?TCP_FASTOPEN_FALSE :: ?TCP_FASTOPEN_FALSE | ?TCP_FASTOPEN_TRUE,
261264
interface = undefined :: undefined | binary(),
262-
unix_socket_path = undefined :: undefined | binary()
265+
unix_socket_path = undefined :: undefined | binary(),
266+
lock_data_ssl_session = ?LOCK_DATA_SSL_SESSION_FALSE ::
267+
?LOCK_DATA_SSL_SESSION_FALSE | ?LOCK_DATA_SSL_SESSION_TRUE
263268
}).
264269

265270
-ifdef(tcp_fastopen_available).
@@ -384,7 +389,8 @@ handle_call(#req{method = Method,
384389
proxy = Proxy,
385390
tcp_fastopen = TCPFastOpen,
386391
interface = Interface,
387-
unix_socket_path = UnixSocketPath},
392+
unix_socket_path = UnixSocketPath,
393+
lock_data_ssl_session = LockDataSslSession},
388394
From,
389395
State=#state{port=Port, reqs=Reqs}) ->
390396
{Self, Ref} = From,
@@ -402,7 +408,8 @@ handle_call(#req{method = Method,
402408
{?proxy, Proxy},
403409
{?tcp_fastopen, TCPFastOpen},
404410
{?interface, Interface},
405-
{?unix_socket_path, UnixSocketPath}],
411+
{?unix_socket_path, UnixSocketPath},
412+
{?lock_data_ssl_session, LockDataSslSession}],
406413
Command = {Self, Ref, Method, Url, Headers, CookieJar, Body, Opts},
407414
true = port_command(Port, term_to_binary(Command)),
408415
Tref = erlang:start_timer(Timeout, self(), {req_timeout, From}),
@@ -573,6 +580,10 @@ opt(interface, Interface, {Req, Errors}) when is_binary(Interface) ->
573580
opt(unix_socket_path, UnixSocketPath, {Req, Errors})
574581
when is_binary(UnixSocketPath) andalso ?UNIX_SOCKET_PATH_AVAILABLE ->
575582
{Req#req{unix_socket_path=UnixSocketPath}, Errors};
583+
opt(lock_data_ssl_session, true, {Req, Errors}) ->
584+
{Req#req{lock_data_ssl_session=?LOCK_DATA_SSL_SESSION_TRUE}, Errors};
585+
opt(lock_data_ssl_session, false, {Req, Errors}) ->
586+
{Req#req{lock_data_ssl_session=?LOCK_DATA_SSL_SESSION_FALSE}, Errors};
576587
opt(K, V, {Req, Errors}) ->
577588
{Req, [{K, V} | Errors]}.
578589

0 commit comments

Comments
 (0)