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