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