Line | Count | Source (jump to first uncovered line) |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | |
25 | | #include "curl_setup.h" |
26 | | |
27 | | #ifndef CURL_DISABLE_GOPHER |
28 | | |
29 | | #include "urldata.h" |
30 | | #include <curl/curl.h> |
31 | | #include "transfer.h" |
32 | | #include "sendf.h" |
33 | | #include "connect.h" |
34 | | #include "progress.h" |
35 | | #include "gopher.h" |
36 | | #include "select.h" |
37 | | #include "strdup.h" |
38 | | #include "vtls/vtls.h" |
39 | | #include "url.h" |
40 | | #include "escape.h" |
41 | | #include "warnless.h" |
42 | | #include "curl_printf.h" |
43 | | #include "curl_memory.h" |
44 | | /* The last #include file should be: */ |
45 | | #include "memdebug.h" |
46 | | |
47 | | /* |
48 | | * Forward declarations. |
49 | | */ |
50 | | |
51 | | static CURLcode gopher_do(struct Curl_easy *data, bool *done); |
52 | | #ifdef USE_SSL |
53 | | static CURLcode gopher_connect(struct Curl_easy *data, bool *done); |
54 | | static CURLcode gopher_connecting(struct Curl_easy *data, bool *done); |
55 | | #endif |
56 | | |
57 | | /* |
58 | | * Gopher protocol handler. |
59 | | * This is also a nice simple template to build off for simple |
60 | | * connect-command-download protocols. |
61 | | */ |
62 | | |
63 | | const struct Curl_handler Curl_handler_gopher = { |
64 | | "GOPHER", /* scheme */ |
65 | | ZERO_NULL, /* setup_connection */ |
66 | | gopher_do, /* do_it */ |
67 | | ZERO_NULL, /* done */ |
68 | | ZERO_NULL, /* do_more */ |
69 | | ZERO_NULL, /* connect_it */ |
70 | | ZERO_NULL, /* connecting */ |
71 | | ZERO_NULL, /* doing */ |
72 | | ZERO_NULL, /* proto_getsock */ |
73 | | ZERO_NULL, /* doing_getsock */ |
74 | | ZERO_NULL, /* domore_getsock */ |
75 | | ZERO_NULL, /* perform_getsock */ |
76 | | ZERO_NULL, /* disconnect */ |
77 | | ZERO_NULL, /* readwrite */ |
78 | | ZERO_NULL, /* connection_check */ |
79 | | ZERO_NULL, /* attach connection */ |
80 | | PORT_GOPHER, /* defport */ |
81 | | CURLPROTO_GOPHER, /* protocol */ |
82 | | CURLPROTO_GOPHER, /* family */ |
83 | | PROTOPT_NONE /* flags */ |
84 | | }; |
85 | | |
86 | | #ifdef USE_SSL |
87 | | const struct Curl_handler Curl_handler_gophers = { |
88 | | "GOPHERS", /* scheme */ |
89 | | ZERO_NULL, /* setup_connection */ |
90 | | gopher_do, /* do_it */ |
91 | | ZERO_NULL, /* done */ |
92 | | ZERO_NULL, /* do_more */ |
93 | | gopher_connect, /* connect_it */ |
94 | | gopher_connecting, /* connecting */ |
95 | | ZERO_NULL, /* doing */ |
96 | | ZERO_NULL, /* proto_getsock */ |
97 | | ZERO_NULL, /* doing_getsock */ |
98 | | ZERO_NULL, /* domore_getsock */ |
99 | | ZERO_NULL, /* perform_getsock */ |
100 | | ZERO_NULL, /* disconnect */ |
101 | | ZERO_NULL, /* readwrite */ |
102 | | ZERO_NULL, /* connection_check */ |
103 | | ZERO_NULL, /* attach connection */ |
104 | | PORT_GOPHER, /* defport */ |
105 | | CURLPROTO_GOPHERS, /* protocol */ |
106 | | CURLPROTO_GOPHER, /* family */ |
107 | | PROTOPT_SSL /* flags */ |
108 | | }; |
109 | | |
110 | | static CURLcode gopher_connect(struct Curl_easy *data, bool *done) |
111 | 2.90k | { |
112 | 2.90k | (void)data; |
113 | 2.90k | (void)done; |
114 | 2.90k | return CURLE_OK; |
115 | 2.90k | } |
116 | | |
117 | | static CURLcode gopher_connecting(struct Curl_easy *data, bool *done) |
118 | 2.90k | { |
119 | 2.90k | struct connectdata *conn = data->conn; |
120 | 2.90k | CURLcode result = Curl_ssl_connect(data, conn, FIRSTSOCKET); |
121 | 2.90k | if(result) |
122 | 2.90k | connclose(conn, "Failed TLS connection"); |
123 | 2.90k | *done = TRUE; |
124 | 2.90k | return result; |
125 | 2.90k | } |
126 | | #endif |
127 | | |
128 | | static CURLcode gopher_do(struct Curl_easy *data, bool *done) |
129 | 1.18k | { |
130 | 1.18k | CURLcode result = CURLE_OK; |
131 | 1.18k | struct connectdata *conn = data->conn; |
132 | 1.18k | curl_socket_t sockfd = conn->sock[FIRSTSOCKET]; |
133 | 1.18k | char *gopherpath; |
134 | 1.18k | char *path = data->state.up.path; |
135 | 1.18k | char *query = data->state.up.query; |
136 | 1.18k | char *sel = NULL; |
137 | 1.18k | char *sel_org = NULL; |
138 | 1.18k | timediff_t timeout_ms; |
139 | 1.18k | ssize_t amount, k; |
140 | 1.18k | size_t len; |
141 | 1.18k | int what; |
142 | | |
143 | 1.18k | *done = TRUE; /* unconditionally */ |
144 | | |
145 | | /* path is guaranteed non-NULL */ |
146 | 1.18k | DEBUGASSERT(path); |
147 | | |
148 | 1.18k | if(query) |
149 | 144 | gopherpath = aprintf("%s?%s", path, query); |
150 | 1.04k | else |
151 | 1.04k | gopherpath = strdup(path); |
152 | | |
153 | 1.18k | if(!gopherpath) |
154 | 0 | return CURLE_OUT_OF_MEMORY; |
155 | | |
156 | | /* Create selector. Degenerate cases: / and /1 => convert to "" */ |
157 | 1.18k | if(strlen(gopherpath) <= 2) { |
158 | 906 | sel = (char *)""; |
159 | 906 | len = strlen(sel); |
160 | 906 | free(gopherpath); |
161 | 906 | } |
162 | 283 | else { |
163 | 283 | char *newp; |
164 | | |
165 | | /* Otherwise, drop / and the first character (i.e., item type) ... */ |
166 | 283 | newp = gopherpath; |
167 | 283 | newp += 2; |
168 | | |
169 | | /* ... and finally unescape */ |
170 | 283 | result = Curl_urldecode(newp, 0, &sel, &len, REJECT_ZERO); |
171 | 283 | free(gopherpath); |
172 | 283 | if(result) |
173 | 1 | return result; |
174 | 282 | sel_org = sel; |
175 | 282 | } |
176 | | |
177 | 1.18k | k = curlx_uztosz(len); |
178 | | |
179 | 1.18k | for(;;) { |
180 | | /* Break out of the loop if the selector is empty because OpenSSL and/or |
181 | | LibreSSL fail with errno 0 if this is the case. */ |
182 | 1.18k | if(strlen(sel) < 1) |
183 | 906 | break; |
184 | | |
185 | 282 | result = Curl_write(data, sockfd, sel, k, &amount); |
186 | 282 | if(!result) { /* Which may not have written it all! */ |
187 | 282 | result = Curl_client_write(data, CLIENTWRITE_HEADER, sel, amount); |
188 | 282 | if(result) |
189 | 0 | break; |
190 | | |
191 | 282 | k -= amount; |
192 | 282 | sel += amount; |
193 | 282 | if(k < 1) |
194 | 243 | break; /* but it did write it all */ |
195 | 282 | } |
196 | 0 | else |
197 | 0 | break; |
198 | | |
199 | 39 | timeout_ms = Curl_timeleft(data, NULL, FALSE); |
200 | 39 | if(timeout_ms < 0) { |
201 | 0 | result = CURLE_OPERATION_TIMEDOUT; |
202 | 0 | break; |
203 | 0 | } |
204 | 39 | if(!timeout_ms) |
205 | 0 | timeout_ms = TIMEDIFF_T_MAX; |
206 | | |
207 | | /* Don't busyloop. The entire loop thing is a work-around as it causes a |
208 | | BLOCKING behavior which is a NO-NO. This function should rather be |
209 | | split up in a do and a doing piece where the pieces that aren't |
210 | | possible to send now will be sent in the doing function repeatedly |
211 | | until the entire request is sent. |
212 | | */ |
213 | 39 | what = SOCKET_WRITABLE(sockfd, timeout_ms); |
214 | 39 | if(what < 0) { |
215 | 0 | result = CURLE_SEND_ERROR; |
216 | 0 | break; |
217 | 0 | } |
218 | 39 | else if(!what) { |
219 | 39 | result = CURLE_OPERATION_TIMEDOUT; |
220 | 39 | break; |
221 | 39 | } |
222 | 39 | } |
223 | | |
224 | 1.18k | free(sel_org); |
225 | | |
226 | 1.18k | if(!result) |
227 | 1.14k | result = Curl_write(data, sockfd, "\r\n", 2, &amount); |
228 | 1.18k | if(result) { |
229 | 39 | failf(data, "Failed sending Gopher request"); |
230 | 39 | return result; |
231 | 39 | } |
232 | 1.14k | result = Curl_client_write(data, CLIENTWRITE_HEADER, (char *)"\r\n", 2); |
233 | 1.14k | if(result) |
234 | 0 | return result; |
235 | | |
236 | 1.14k | Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1); |
237 | 1.14k | return CURLE_OK; |
238 | 1.14k | } |
239 | | #endif /*CURL_DISABLE_GOPHER*/ |