1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2020, 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.haxx.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 "url.h"
37 #include "escape.h"
38 #include "warnless.h"
39 #include "curl_printf.h"
40 #include "curl_memory.h"
41 /* The last #include file should be: */
42 #include "memdebug.h"
43
44 /*
45 * Forward declarations.
46 */
47
48 static CURLcode gopher_do(struct connectdata *conn, bool *done);
49
50 /*
51 * Gopher protocol handler.
52 * This is also a nice simple template to build off for simple
53 * connect-command-download protocols.
54 */
55
56 const struct Curl_handler Curl_handler_gopher = {
57 "GOPHER", /* scheme */
58 ZERO_NULL, /* setup_connection */
59 gopher_do, /* do_it */
60 ZERO_NULL, /* done */
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 */
76 };
77
gopher_do(struct connectdata * conn,bool * done)78 static CURLcode gopher_do(struct connectdata *conn, bool *done)
79 {
80 CURLcode result = CURLE_OK;
81 struct Curl_easy *data = conn->data;
82 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
83 char *gopherpath;
84 char *path = data->state.up.path;
85 char *query = data->state.up.query;
86 char *sel = NULL;
87 char *sel_org = NULL;
88 timediff_t timeout_ms;
89 ssize_t amount, k;
90 size_t len;
91 int what;
92
93 *done = TRUE; /* unconditionally */
94
95 /* path is guaranteed non-NULL */
96 DEBUGASSERT(path);
97
98 if(query)
99 gopherpath = aprintf("%s?%s", path, query);
100 else
101 gopherpath = strdup(path);
102
103 if(!gopherpath)
104 return CURLE_OUT_OF_MEMORY;
105
106 /* Create selector. Degenerate cases: / and /1 => convert to "" */
107 if(strlen(gopherpath) <= 2) {
108 sel = (char *)"";
109 len = strlen(sel);
110 free(gopherpath);
111 }
112 else {
113 char *newp;
114
115 /* Otherwise, drop / and the first character (i.e., item type) ... */
116 newp = gopherpath;
117 newp += 2;
118
119 /* ... and finally unescape */
120 result = Curl_urldecode(data, newp, 0, &sel, &len, REJECT_ZERO);
121 free(gopherpath);
122 if(result)
123 return result;
124 sel_org = sel;
125 }
126
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);
130
131 for(;;) {
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);
135 if(result)
136 break;
137
138 k -= amount;
139 sel += amount;
140 if(k < 1)
141 break; /* but it did write it all */
142 }
143 else
144 break;
145
146 timeout_ms = Curl_timeleft(conn->data, NULL, FALSE);
147 if(timeout_ms < 0) {
148 result = CURLE_OPERATION_TIMEDOUT;
149 break;
150 }
151 if(!timeout_ms)
152 timeout_ms = TIMEDIFF_T_MAX;
153
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.
159 */
160 what = SOCKET_WRITABLE(sockfd, timeout_ms);
161 if(what < 0) {
162 result = CURLE_SEND_ERROR;
163 break;
164 }
165 else if(!what) {
166 result = CURLE_OPERATION_TIMEDOUT;
167 break;
168 }
169 }
170
171 free(sel_org);
172
173 if(!result)
174 result = Curl_write(conn, sockfd, "\r\n", 2, &amount);
175 if(result) {
176 failf(data, "Failed sending Gopher request");
177 return result;
178 }
179 result = Curl_client_write(conn, CLIENTWRITE_HEADER, (char *)"\r\n", 2);
180 if(result)
181 return result;
182
183 Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
184 return CURLE_OK;
185 }
186 #endif /*CURL_DISABLE_GOPHER*/
187