1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2020, 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.haxx.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 ***************************************************************************/
23 #include "curl_setup.h"
25 #ifndef CURL_DISABLE_GOPHER
28 #include <curl/curl.h>
39 #include "curl_printf.h"
40 #include "curl_memory.h"
41 /* The last #include file should be: */
45 * Forward declarations.
48 static CURLcode gopher_do(struct connectdata *conn, bool *done);
51 * Gopher protocol handler.
52 * This is also a nice simple template to build off for simple
53 * connect-command-download protocols.
56 const struct Curl_handler Curl_handler_gopher = {
57 "GOPHER", /* scheme */
58 ZERO_NULL, /* setup_connection */
59 gopher_do, /* do_it */
61 ZERO_NULL, /* do_more */
62 ZERO_NULL, /* connect_it */
63 ZERO_NULL, /* connecting */
64 ZERO_NULL, /* doing */
65 ZERO_NULL, /* proto_getsock */
66 ZERO_NULL, /* doing_getsock */
67 ZERO_NULL, /* domore_getsock */
68 ZERO_NULL, /* perform_getsock */
69 ZERO_NULL, /* disconnect */
70 ZERO_NULL, /* readwrite */
71 ZERO_NULL, /* connection_check */
72 PORT_GOPHER, /* defport */
73 CURLPROTO_GOPHER, /* protocol */
74 CURLPROTO_GOPHER, /* family */
75 PROTOPT_NONE /* flags */
78 static CURLcode gopher_do(struct connectdata *conn, bool *done)
80 CURLcode result = CURLE_OK;
81 struct Curl_easy *data = conn->data;
82 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
84 char *path = data->state.up.path;
85 char *query = data->state.up.query;
88 timediff_t timeout_ms;
93 *done = TRUE; /* unconditionally */
95 /* path is guaranteed non-NULL */
99 gopherpath = aprintf("%s?%s", path, query);
101 gopherpath = strdup(path);
104 return CURLE_OUT_OF_MEMORY;
106 /* Create selector. Degenerate cases: / and /1 => convert to "" */
107 if(strlen(gopherpath) <= 2) {
115 /* Otherwise, drop / and the first character (i.e., item type) ... */
119 /* ... and finally unescape */
120 result = Curl_urldecode(data, newp, 0, &sel, &len, REJECT_ZERO);
127 /* We use Curl_write instead of Curl_sendf to make sure the entire buffer is
128 sent, which could be sizeable with long selectors. */
129 k = curlx_uztosz(len);
132 result = Curl_write(conn, sockfd, sel, k, &amount);
133 if(!result) { /* Which may not have written it all! */
134 result = Curl_client_write(conn, CLIENTWRITE_HEADER, sel, amount);
141 break; /* but it did write it all */
146 timeout_ms = Curl_timeleft(conn->data, NULL, FALSE);
148 result = CURLE_OPERATION_TIMEDOUT;
152 timeout_ms = TIMEDIFF_T_MAX;
154 /* Don't busyloop. The entire loop thing is a work-around as it causes a
155 BLOCKING behavior which is a NO-NO. This function should rather be
156 split up in a do and a doing piece where the pieces that aren't
157 possible to send now will be sent in the doing function repeatedly
158 until the entire request is sent.
160 what = SOCKET_WRITABLE(sockfd, timeout_ms);
162 result = CURLE_SEND_ERROR;
166 result = CURLE_OPERATION_TIMEDOUT;
174 result = Curl_write(conn, sockfd, "\r\n", 2, &amount);
176 failf(data, "Failed sending Gopher request");
179 result = Curl_client_write(conn, CLIENTWRITE_HEADER, (char *)"\r\n", 2);
183 Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
186 #endif /*CURL_DISABLE_GOPHER*/