1 | /*************************************************************************** |
2 | * _ _ ____ _ |
3 | * Project ___| | | | _ \| | |
4 | * / __| | | | |_) | | |
5 | * | (__| |_| | _ <| |___ |
6 | * \___|\___/|_| \_\_____| |
7 | * |
8 | * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, 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 | { |
112 | (void)data; |
113 | (void)done; |
114 | return CURLE_OK; |
115 | } |
116 | |
117 | static CURLcode gopher_connecting(struct Curl_easy *data, bool *done) |
118 | { |
119 | struct connectdata *conn = data->conn; |
120 | CURLcode result = Curl_ssl_connect(data, conn, FIRSTSOCKET); |
121 | if(result) |
122 | connclose(conn, "Failed TLS connection" ); |
123 | *done = TRUE; |
124 | return result; |
125 | } |
126 | #endif |
127 | |
128 | static CURLcode gopher_do(struct Curl_easy *data, bool *done) |
129 | { |
130 | CURLcode result = CURLE_OK; |
131 | struct connectdata *conn = data->conn; |
132 | curl_socket_t sockfd = conn->sock[FIRSTSOCKET]; |
133 | char *gopherpath; |
134 | char *path = data->state.up.path; |
135 | char *query = data->state.up.query; |
136 | char *sel = NULL; |
137 | char *sel_org = NULL; |
138 | timediff_t timeout_ms; |
139 | ssize_t amount, k; |
140 | size_t len; |
141 | int what; |
142 | |
143 | *done = TRUE; /* unconditionally */ |
144 | |
145 | /* path is guaranteed non-NULL */ |
146 | DEBUGASSERT(path); |
147 | |
148 | if(query) |
149 | gopherpath = aprintf("%s?%s" , path, query); |
150 | else |
151 | gopherpath = strdup(path); |
152 | |
153 | if(!gopherpath) |
154 | return CURLE_OUT_OF_MEMORY; |
155 | |
156 | /* Create selector. Degenerate cases: / and /1 => convert to "" */ |
157 | if(strlen(gopherpath) <= 2) { |
158 | sel = (char *)"" ; |
159 | len = strlen(sel); |
160 | free(gopherpath); |
161 | } |
162 | else { |
163 | char *newp; |
164 | |
165 | /* Otherwise, drop / and the first character (i.e., item type) ... */ |
166 | newp = gopherpath; |
167 | newp += 2; |
168 | |
169 | /* ... and finally unescape */ |
170 | result = Curl_urldecode(newp, 0, &sel, &len, REJECT_ZERO); |
171 | free(gopherpath); |
172 | if(result) |
173 | return result; |
174 | sel_org = sel; |
175 | } |
176 | |
177 | k = curlx_uztosz(len); |
178 | |
179 | 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 | if(strlen(sel) < 1) |
183 | break; |
184 | |
185 | result = Curl_write(data, sockfd, sel, k, &amount); |
186 | if(!result) { /* Which may not have written it all! */ |
187 | result = Curl_client_write(data, CLIENTWRITE_HEADER, sel, amount); |
188 | if(result) |
189 | break; |
190 | |
191 | k -= amount; |
192 | sel += amount; |
193 | if(k < 1) |
194 | break; /* but it did write it all */ |
195 | } |
196 | else |
197 | break; |
198 | |
199 | timeout_ms = Curl_timeleft(data, NULL, FALSE); |
200 | if(timeout_ms < 0) { |
201 | result = CURLE_OPERATION_TIMEDOUT; |
202 | break; |
203 | } |
204 | if(!timeout_ms) |
205 | 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 | what = SOCKET_WRITABLE(sockfd, timeout_ms); |
214 | if(what < 0) { |
215 | result = CURLE_SEND_ERROR; |
216 | break; |
217 | } |
218 | else if(!what) { |
219 | result = CURLE_OPERATION_TIMEDOUT; |
220 | break; |
221 | } |
222 | } |
223 | |
224 | free(sel_org); |
225 | |
226 | if(!result) |
227 | result = Curl_write(data, sockfd, "\r\n" , 2, &amount); |
228 | if(result) { |
229 | failf(data, "Failed sending Gopher request" ); |
230 | return result; |
231 | } |
232 | result = Curl_client_write(data, CLIENTWRITE_HEADER, (char *)"\r\n" , 2); |
233 | if(result) |
234 | return result; |
235 | |
236 | Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1); |
237 | return CURLE_OK; |
238 | } |
239 | #endif /*CURL_DISABLE_GOPHER*/ |
240 | |