/src/PROJ/curl/lib/gopher.c
Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) 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 "cfilters.h" |
34 | | #include "connect.h" |
35 | | #include "progress.h" |
36 | | #include "gopher.h" |
37 | | #include "select.h" |
38 | | #include "vtls/vtls.h" |
39 | | #include "url.h" |
40 | | #include "escape.h" |
41 | | #include "curlx/warnless.h" |
42 | | |
43 | | /* The last 2 #include files should be: */ |
44 | | #include "curl_memory.h" |
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_pollset */ |
73 | | ZERO_NULL, /* doing_pollset */ |
74 | | ZERO_NULL, /* domore_pollset */ |
75 | | ZERO_NULL, /* perform_pollset */ |
76 | | ZERO_NULL, /* disconnect */ |
77 | | ZERO_NULL, /* write_resp */ |
78 | | ZERO_NULL, /* write_resp_hd */ |
79 | | ZERO_NULL, /* connection_check */ |
80 | | ZERO_NULL, /* attach connection */ |
81 | | ZERO_NULL, /* follow */ |
82 | | PORT_GOPHER, /* defport */ |
83 | | CURLPROTO_GOPHER, /* protocol */ |
84 | | CURLPROTO_GOPHER, /* family */ |
85 | | PROTOPT_NONE /* flags */ |
86 | | }; |
87 | | |
88 | | #ifdef USE_SSL |
89 | | const struct Curl_handler Curl_handler_gophers = { |
90 | | "gophers", /* scheme */ |
91 | | ZERO_NULL, /* setup_connection */ |
92 | | gopher_do, /* do_it */ |
93 | | ZERO_NULL, /* done */ |
94 | | ZERO_NULL, /* do_more */ |
95 | | gopher_connect, /* connect_it */ |
96 | | gopher_connecting, /* connecting */ |
97 | | ZERO_NULL, /* doing */ |
98 | | ZERO_NULL, /* proto_pollset */ |
99 | | ZERO_NULL, /* doing_pollset */ |
100 | | ZERO_NULL, /* domore_pollset */ |
101 | | ZERO_NULL, /* perform_pollset */ |
102 | | ZERO_NULL, /* disconnect */ |
103 | | ZERO_NULL, /* write_resp */ |
104 | | ZERO_NULL, /* write_resp_hd */ |
105 | | ZERO_NULL, /* connection_check */ |
106 | | ZERO_NULL, /* attach connection */ |
107 | | ZERO_NULL, /* follow */ |
108 | | PORT_GOPHER, /* defport */ |
109 | | CURLPROTO_GOPHERS, /* protocol */ |
110 | | CURLPROTO_GOPHER, /* family */ |
111 | | PROTOPT_SSL /* flags */ |
112 | | }; |
113 | | |
114 | | static CURLcode gopher_connect(struct Curl_easy *data, bool *done) |
115 | 0 | { |
116 | 0 | (void)data; |
117 | 0 | (void)done; |
118 | 0 | return CURLE_OK; |
119 | 0 | } |
120 | | |
121 | | static CURLcode gopher_connecting(struct Curl_easy *data, bool *done) |
122 | 0 | { |
123 | 0 | struct connectdata *conn = data->conn; |
124 | 0 | CURLcode result; |
125 | |
|
126 | 0 | result = Curl_conn_connect(data, FIRSTSOCKET, TRUE, done); |
127 | 0 | if(result) |
128 | 0 | connclose(conn, "Failed TLS connection"); |
129 | 0 | *done = TRUE; |
130 | 0 | return result; |
131 | 0 | } |
132 | | #endif |
133 | | |
134 | | static CURLcode gopher_do(struct Curl_easy *data, bool *done) |
135 | 0 | { |
136 | 0 | CURLcode result = CURLE_OK; |
137 | 0 | struct connectdata *conn = data->conn; |
138 | 0 | curl_socket_t sockfd = conn->sock[FIRSTSOCKET]; |
139 | 0 | char *gopherpath; |
140 | 0 | char *path = data->state.up.path; |
141 | 0 | char *query = data->state.up.query; |
142 | 0 | char *sel = NULL; |
143 | 0 | char *sel_org = NULL; |
144 | 0 | timediff_t timeout_ms; |
145 | 0 | ssize_t k; |
146 | 0 | size_t amount, len; |
147 | 0 | int what; |
148 | |
|
149 | 0 | *done = TRUE; /* unconditionally */ |
150 | | |
151 | | /* path is guaranteed non-NULL */ |
152 | 0 | DEBUGASSERT(path); |
153 | |
|
154 | 0 | if(query) |
155 | 0 | gopherpath = curl_maprintf("%s?%s", path, query); |
156 | 0 | else |
157 | 0 | gopherpath = strdup(path); |
158 | |
|
159 | 0 | if(!gopherpath) |
160 | 0 | return CURLE_OUT_OF_MEMORY; |
161 | | |
162 | | /* Create selector. Degenerate cases: / and /1 => convert to "" */ |
163 | 0 | if(strlen(gopherpath) <= 2) { |
164 | 0 | sel = (char *)CURL_UNCONST(""); |
165 | 0 | len = strlen(sel); |
166 | 0 | free(gopherpath); |
167 | 0 | } |
168 | 0 | else { |
169 | 0 | char *newp; |
170 | | |
171 | | /* Otherwise, drop / and the first character (i.e., item type) ... */ |
172 | 0 | newp = gopherpath; |
173 | 0 | newp += 2; |
174 | | |
175 | | /* ... and finally unescape */ |
176 | 0 | result = Curl_urldecode(newp, 0, &sel, &len, REJECT_ZERO); |
177 | 0 | free(gopherpath); |
178 | 0 | if(result) |
179 | 0 | return result; |
180 | 0 | sel_org = sel; |
181 | 0 | } |
182 | | |
183 | 0 | k = curlx_uztosz(len); |
184 | |
|
185 | 0 | for(;;) { |
186 | | /* Break out of the loop if the selector is empty because OpenSSL and/or |
187 | | LibreSSL fail with errno 0 if this is the case. */ |
188 | 0 | if(strlen(sel) < 1) |
189 | 0 | break; |
190 | | |
191 | 0 | result = Curl_xfer_send(data, sel, k, FALSE, &amount); |
192 | 0 | if(!result) { /* Which may not have written it all! */ |
193 | 0 | result = Curl_client_write(data, CLIENTWRITE_HEADER, sel, amount); |
194 | 0 | if(result) |
195 | 0 | break; |
196 | | |
197 | 0 | k -= amount; |
198 | 0 | sel += amount; |
199 | 0 | if(k < 1) |
200 | 0 | break; /* but it did write it all */ |
201 | 0 | } |
202 | 0 | else |
203 | 0 | break; |
204 | | |
205 | 0 | timeout_ms = Curl_timeleft(data, NULL, FALSE); |
206 | 0 | if(timeout_ms < 0) { |
207 | 0 | result = CURLE_OPERATION_TIMEDOUT; |
208 | 0 | break; |
209 | 0 | } |
210 | 0 | if(!timeout_ms) |
211 | 0 | timeout_ms = TIMEDIFF_T_MAX; |
212 | | |
213 | | /* Do not busyloop. The entire loop thing is a work-around as it causes a |
214 | | BLOCKING behavior which is a NO-NO. This function should rather be |
215 | | split up in a do and a doing piece where the pieces that are not |
216 | | possible to send now will be sent in the doing function repeatedly |
217 | | until the entire request is sent. |
218 | | */ |
219 | 0 | what = SOCKET_WRITABLE(sockfd, timeout_ms); |
220 | 0 | if(what < 0) { |
221 | 0 | result = CURLE_SEND_ERROR; |
222 | 0 | break; |
223 | 0 | } |
224 | 0 | else if(!what) { |
225 | 0 | result = CURLE_OPERATION_TIMEDOUT; |
226 | 0 | break; |
227 | 0 | } |
228 | 0 | } |
229 | |
|
230 | 0 | free(sel_org); |
231 | |
|
232 | 0 | if(!result) |
233 | 0 | result = Curl_xfer_send(data, "\r\n", 2, FALSE, &amount); |
234 | 0 | if(result) { |
235 | 0 | failf(data, "Failed sending Gopher request"); |
236 | 0 | return result; |
237 | 0 | } |
238 | 0 | result = Curl_client_write(data, CLIENTWRITE_HEADER, "\r\n", 2); |
239 | 0 | if(result) |
240 | 0 | return result; |
241 | | |
242 | 0 | Curl_xfer_setup_recv(data, FIRSTSOCKET, -1); |
243 | 0 | return CURLE_OK; |
244 | 0 | } |
245 | | #endif /* CURL_DISABLE_GOPHER */ |