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