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