1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * SPDX-License-Identifier: curl
23 ***************************************************************************/
25 #include "curl_setup.h"
27 #ifndef CURL_DISABLE_GOPHER
30 #include <curl/curl.h>
38 #include "vtls/vtls.h"
42 #include "curl_printf.h"
43 #include "curl_memory.h"
44 /* The last #include file should be: */
48 * Forward declarations.
51 static CURLcode gopher_do(struct Curl_easy *data, bool *done);
53 static CURLcode gopher_connect(struct Curl_easy *data, bool *done);
54 static CURLcode gopher_connecting(struct Curl_easy *data, bool *done);
58 * Gopher protocol handler.
59 * This is also a nice simple template to build off for simple
60 * connect-command-download protocols.
63 const struct Curl_handler Curl_handler_gopher = {
64 "GOPHER", /* scheme */
65 ZERO_NULL, /* setup_connection */
66 gopher_do, /* do_it */
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 */
87 const struct Curl_handler Curl_handler_gophers = {
88 "GOPHERS", /* scheme */
89 ZERO_NULL, /* setup_connection */
90 gopher_do, /* do_it */
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 */
110 static CURLcode gopher_connect(struct Curl_easy *data, bool *done)
117 static CURLcode gopher_connecting(struct Curl_easy *data, bool *done)
119 struct connectdata *conn = data->conn;
120 CURLcode result = Curl_ssl_connect(data, conn, FIRSTSOCKET);
122 connclose(conn, "Failed TLS connection");
128 static CURLcode gopher_do(struct Curl_easy *data, bool *done)
130 CURLcode result = CURLE_OK;
131 struct connectdata *conn = data->conn;
132 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
134 char *path = data->state.up.path;
135 char *query = data->state.up.query;
137 char *sel_org = NULL;
138 timediff_t timeout_ms;
143 *done = TRUE; /* unconditionally */
145 /* path is guaranteed non-NULL */
149 gopherpath = aprintf("%s?%s", path, query);
151 gopherpath = strdup(path);
154 return CURLE_OUT_OF_MEMORY;
156 /* Create selector. Degenerate cases: / and /1 => convert to "" */
157 if(strlen(gopherpath) <= 2) {
165 /* Otherwise, drop / and the first character (i.e., item type) ... */
169 /* ... and finally unescape */
170 result = Curl_urldecode(newp, 0, &sel, &len, REJECT_ZERO);
177 k = curlx_uztosz(len);
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. */
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);
194 break; /* but it did write it all */
199 timeout_ms = Curl_timeleft(data, NULL, FALSE);
201 result = CURLE_OPERATION_TIMEDOUT;
205 timeout_ms = TIMEDIFF_T_MAX;
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.
213 what = SOCKET_WRITABLE(sockfd, timeout_ms);
215 result = CURLE_SEND_ERROR;
219 result = CURLE_OPERATION_TIMEDOUT;
227 result = Curl_write(data, sockfd, "\r\n", 2, &amount);
229 failf(data, "Failed sending Gopher request");
232 result = Curl_client_write(data, CLIENTWRITE_HEADER, (char *)"\r\n", 2);
236 Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
239 #endif /*CURL_DISABLE_GOPHER*/