Line | Count | Source |
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 | | #include "curl_setup.h" |
25 | | #include "urldata.h" |
26 | | #include "gopher.h" |
27 | | |
28 | | #ifndef CURL_DISABLE_GOPHER |
29 | | |
30 | | #include "transfer.h" |
31 | | #include "sendf.h" |
32 | | #include "curl_trc.h" |
33 | | #include "cfilters.h" |
34 | | #include "connect.h" |
35 | | #include "select.h" |
36 | | #include "url.h" |
37 | | #include "escape.h" |
38 | | |
39 | | #ifdef USE_SSL |
40 | | static CURLcode gopher_connect(struct Curl_easy *data, bool *done) |
41 | 0 | { |
42 | 0 | (void)data; |
43 | 0 | (void)done; |
44 | 0 | return CURLE_OK; |
45 | 0 | } |
46 | | |
47 | | static CURLcode gopher_connecting(struct Curl_easy *data, bool *done) |
48 | 0 | { |
49 | 0 | struct connectdata *conn = data->conn; |
50 | 0 | CURLcode result; |
51 | |
|
52 | 0 | result = Curl_conn_connect(data, FIRSTSOCKET, TRUE, done); |
53 | 0 | if(result) |
54 | 0 | connclose(conn); |
55 | 0 | *done = TRUE; |
56 | 0 | return result; |
57 | 0 | } |
58 | | #endif |
59 | | |
60 | | /* Sends buf to the server and, optionally, writes it to the client too. */ |
61 | | static CURLcode send_buf(struct Curl_easy *data, |
62 | | const char *buf, |
63 | | size_t buf_len, |
64 | | bool client_write) |
65 | 0 | { |
66 | 0 | CURLcode result = CURLE_OK; |
67 | 0 | struct connectdata *conn = data->conn; |
68 | 0 | curl_socket_t sockfd = conn->sock[FIRSTSOCKET]; |
69 | 0 | size_t nwritten; |
70 | 0 | timediff_t timeout_ms; |
71 | 0 | int what; |
72 | |
|
73 | 0 | while(buf_len) { |
74 | 0 | result = Curl_xfer_send(data, buf, buf_len, FALSE, &nwritten); |
75 | 0 | if(!result) { /* Which may not have written it all! */ |
76 | 0 | if(client_write) { |
77 | 0 | result = Curl_client_write(data, CLIENTWRITE_HEADER, buf, nwritten); |
78 | 0 | if(result) |
79 | 0 | break; |
80 | 0 | } |
81 | | |
82 | 0 | if(nwritten > buf_len) { |
83 | 0 | DEBUGASSERT(0); |
84 | 0 | break; |
85 | 0 | } |
86 | 0 | buf_len -= nwritten; |
87 | 0 | buf += nwritten; |
88 | 0 | if(!buf_len) |
89 | 0 | break; /* but it did write it all */ |
90 | 0 | } |
91 | 0 | else |
92 | 0 | break; |
93 | | |
94 | 0 | timeout_ms = Curl_timeleft_ms(data); |
95 | 0 | if(timeout_ms < 0) { |
96 | 0 | result = CURLE_OPERATION_TIMEDOUT; |
97 | 0 | break; |
98 | 0 | } |
99 | 0 | if(!timeout_ms) |
100 | 0 | timeout_ms = TIMEDIFF_T_MAX; |
101 | | |
102 | | /* Do not busyloop. The entire loop thing is a workaround as it causes a |
103 | | BLOCKING behavior which is a NO-NO. This function should rather be |
104 | | split up in a do and a doing piece where the pieces that are not |
105 | | possible to send now will be sent in the doing function repeatedly |
106 | | until the entire request is sent. */ |
107 | 0 | what = SOCKET_WRITABLE(sockfd, timeout_ms); |
108 | 0 | if(what < 0) { |
109 | 0 | result = CURLE_SEND_ERROR; |
110 | 0 | break; |
111 | 0 | } |
112 | 0 | else if(!what) { |
113 | 0 | result = CURLE_OPERATION_TIMEDOUT; |
114 | 0 | break; |
115 | 0 | } |
116 | 0 | } |
117 | | |
118 | 0 | return result; |
119 | 0 | } |
120 | | |
121 | | static CURLcode gopher_do(struct Curl_easy *data, bool *done) |
122 | 0 | { |
123 | 0 | CURLcode result = CURLE_OK; |
124 | 0 | char *gopherpath; |
125 | 0 | const char *path = data->state.up.path; |
126 | 0 | const char *query = data->state.up.query; |
127 | 0 | const char *buf = NULL; |
128 | 0 | char *buf_alloc = NULL; |
129 | 0 | size_t buf_len; |
130 | |
|
131 | 0 | *done = TRUE; /* unconditionally */ |
132 | | |
133 | | /* path is guaranteed non-NULL */ |
134 | 0 | DEBUGASSERT(path); |
135 | |
|
136 | 0 | if(query) |
137 | 0 | gopherpath = curl_maprintf("%s?%s", path, query); |
138 | 0 | else |
139 | 0 | gopherpath = curlx_strdup(path); |
140 | |
|
141 | 0 | if(!gopherpath) |
142 | 0 | return CURLE_OUT_OF_MEMORY; |
143 | | |
144 | | /* Create selector. Degenerate cases: / and /1 => convert to "" */ |
145 | 0 | if(strlen(gopherpath) <= 2) { |
146 | 0 | buf = ""; |
147 | 0 | buf_len = 0; |
148 | 0 | curlx_free(gopherpath); |
149 | 0 | } |
150 | 0 | else { |
151 | 0 | const char *newp; |
152 | | |
153 | | /* Otherwise, drop / and the first character (i.e., item type) ... */ |
154 | 0 | newp = gopherpath; |
155 | 0 | newp += 2; |
156 | | |
157 | | /* ... and finally unescape */ |
158 | 0 | result = Curl_urldecode(newp, 0, &buf_alloc, &buf_len, REJECT_ZERO); |
159 | 0 | curlx_free(gopherpath); |
160 | 0 | if(result) |
161 | 0 | return result; |
162 | 0 | buf = buf_alloc; |
163 | | |
164 | | /* A decoded CR or LF would terminate the single-line gopher request and |
165 | | let a crafted URL smuggle additional bytes onto the wire. REJECT_ZERO |
166 | | only blocks NUL; reject CR and LF here too. A TAB is left alone as it |
167 | | is the legitimate gopher type-7 selector/search separator. */ |
168 | 0 | if(memchr(buf, '\r', buf_len) || memchr(buf, '\n', buf_len)) { |
169 | 0 | curlx_free(buf_alloc); |
170 | 0 | failf(data, "Bad gopher selector, CR or LF not allowed"); |
171 | 0 | return CURLE_URL_MALFORMAT; |
172 | 0 | } |
173 | 0 | } |
174 | | |
175 | 0 | result = send_buf(data, buf, buf_len, TRUE); |
176 | 0 | curlx_free(buf_alloc); |
177 | |
|
178 | 0 | if(!result) |
179 | | /* Send CRLF to the server now, but defer writing it to the client to |
180 | | preserve the historical behavior of this file. */ |
181 | 0 | result = send_buf(data, "\r\n", 2, FALSE); |
182 | 0 | if(result) { |
183 | 0 | failf(data, "Failed sending Gopher request"); |
184 | 0 | return result; |
185 | 0 | } |
186 | 0 | result = Curl_client_write(data, CLIENTWRITE_HEADER, "\r\n", 2); |
187 | 0 | if(result) |
188 | 0 | return result; |
189 | | |
190 | 0 | Curl_xfer_setup_recv(data, FIRSTSOCKET, -1); |
191 | 0 | return CURLE_OK; |
192 | 0 | } |
193 | | |
194 | | /* |
195 | | * Gopher protocol handler. |
196 | | * This is also a nice simple template to build off for simple |
197 | | * connect-command-download protocols. |
198 | | */ |
199 | | |
200 | | const struct Curl_protocol Curl_protocol_gopher = { |
201 | | ZERO_NULL, /* setup_connection */ |
202 | | gopher_do, /* do_it */ |
203 | | ZERO_NULL, /* done */ |
204 | | ZERO_NULL, /* do_more */ |
205 | | ZERO_NULL, /* connect_it */ |
206 | | ZERO_NULL, /* connecting */ |
207 | | ZERO_NULL, /* doing */ |
208 | | ZERO_NULL, /* proto_pollset */ |
209 | | ZERO_NULL, /* doing_pollset */ |
210 | | ZERO_NULL, /* domore_pollset */ |
211 | | ZERO_NULL, /* perform_pollset */ |
212 | | ZERO_NULL, /* disconnect */ |
213 | | ZERO_NULL, /* write_resp */ |
214 | | ZERO_NULL, /* write_resp_hd */ |
215 | | ZERO_NULL, /* connection_is_dead */ |
216 | | ZERO_NULL, /* attach connection */ |
217 | | ZERO_NULL, /* follow */ |
218 | | }; |
219 | | |
220 | | #ifdef USE_SSL |
221 | | const struct Curl_protocol Curl_protocol_gophers = { |
222 | | ZERO_NULL, /* setup_connection */ |
223 | | gopher_do, /* do_it */ |
224 | | ZERO_NULL, /* done */ |
225 | | ZERO_NULL, /* do_more */ |
226 | | gopher_connect, /* connect_it */ |
227 | | gopher_connecting, /* connecting */ |
228 | | ZERO_NULL, /* doing */ |
229 | | ZERO_NULL, /* proto_pollset */ |
230 | | ZERO_NULL, /* doing_pollset */ |
231 | | ZERO_NULL, /* domore_pollset */ |
232 | | ZERO_NULL, /* perform_pollset */ |
233 | | ZERO_NULL, /* disconnect */ |
234 | | ZERO_NULL, /* write_resp */ |
235 | | ZERO_NULL, /* write_resp_hd */ |
236 | | ZERO_NULL, /* connection_is_dead */ |
237 | | ZERO_NULL, /* attach connection */ |
238 | | ZERO_NULL, /* follow */ |
239 | | }; |
240 | | #endif |
241 | | |
242 | | #endif /* CURL_DISABLE_GOPHER */ |