blob: b7c31b695e4027568eec169b674500370abe65e0 [file] [log] [blame]
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
Alex Deymo486467e2017-12-19 19:04:07 +01008 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
Lucas Eckels9bd90e62012-08-06 15:07:02 -07009 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
Alex Deymod15eaac2016-06-28 14:49:26 -070012 * are also available at https://curl.haxx.se/docs/copyright.html.
Lucas Eckels9bd90e62012-08-06 15:07:02 -070013 *
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
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070023#include "curl_setup.h"
Lucas Eckels9bd90e62012-08-06 15:07:02 -070024
25#ifndef CURL_DISABLE_GOPHER
26
Lucas Eckels9bd90e62012-08-06 15:07:02 -070027#include "urldata.h"
28#include <curl/curl.h>
29#include "transfer.h"
30#include "sendf.h"
Lucas Eckels9bd90e62012-08-06 15:07:02 -070031#include "progress.h"
Lucas Eckels9bd90e62012-08-06 15:07:02 -070032#include "gopher.h"
Lucas Eckels9bd90e62012-08-06 15:07:02 -070033#include "select.h"
34#include "url.h"
Elliott Hughescee03382017-06-23 12:17:18 -070035#include "escape.h"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070036#include "warnless.h"
37#include "curl_memory.h"
Lucas Eckels9bd90e62012-08-06 15:07:02 -070038/* The last #include file should be: */
39#include "memdebug.h"
40
Lucas Eckels9bd90e62012-08-06 15:07:02 -070041/*
42 * Forward declarations.
43 */
44
45static CURLcode gopher_do(struct connectdata *conn, bool *done);
46
47/*
48 * Gopher protocol handler.
49 * This is also a nice simple template to build off for simple
50 * connect-command-download protocols.
51 */
52
53const struct Curl_handler Curl_handler_gopher = {
54 "GOPHER", /* scheme */
55 ZERO_NULL, /* setup_connection */
56 gopher_do, /* do_it */
57 ZERO_NULL, /* done */
58 ZERO_NULL, /* do_more */
59 ZERO_NULL, /* connect_it */
60 ZERO_NULL, /* connecting */
61 ZERO_NULL, /* doing */
62 ZERO_NULL, /* proto_getsock */
63 ZERO_NULL, /* doing_getsock */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070064 ZERO_NULL, /* domore_getsock */
Lucas Eckels9bd90e62012-08-06 15:07:02 -070065 ZERO_NULL, /* perform_getsock */
66 ZERO_NULL, /* disconnect */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070067 ZERO_NULL, /* readwrite */
Elliott Hughes82be86d2017-09-20 17:00:17 -070068 ZERO_NULL, /* connection_check */
Lucas Eckels9bd90e62012-08-06 15:07:02 -070069 PORT_GOPHER, /* defport */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070070 CURLPROTO_GOPHER, /* protocol */
71 PROTOPT_NONE /* flags */
Lucas Eckels9bd90e62012-08-06 15:07:02 -070072};
73
74static CURLcode gopher_do(struct connectdata *conn, bool *done)
75{
Alex Deymo486467e2017-12-19 19:04:07 +010076 CURLcode result = CURLE_OK;
77 struct Curl_easy *data = conn->data;
Lucas Eckels9bd90e62012-08-06 15:07:02 -070078 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
79
80 curl_off_t *bytecount = &data->req.bytecount;
81 char *path = data->state.path;
Elliott Hughes82be86d2017-09-20 17:00:17 -070082 char *sel = NULL;
Lucas Eckels9bd90e62012-08-06 15:07:02 -070083 char *sel_org = NULL;
84 ssize_t amount, k;
Elliott Hughescee03382017-06-23 12:17:18 -070085 size_t len;
Lucas Eckels9bd90e62012-08-06 15:07:02 -070086
87 *done = TRUE; /* unconditionally */
88
89 /* Create selector. Degenerate cases: / and /1 => convert to "" */
Alex Deymod15eaac2016-06-28 14:49:26 -070090 if(strlen(path) <= 2) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -070091 sel = (char *)"";
Alex Deymod15eaac2016-06-28 14:49:26 -070092 len = (int)strlen(sel);
93 }
Lucas Eckels9bd90e62012-08-06 15:07:02 -070094 else {
95 char *newp;
96 size_t j, i;
Lucas Eckels9bd90e62012-08-06 15:07:02 -070097
98 /* Otherwise, drop / and the first character (i.e., item type) ... */
99 newp = path;
Alex Deymo486467e2017-12-19 19:04:07 +0100100 newp += 2;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700101
102 /* ... then turn ? into TAB for search servers, Veronica, etc. ... */
103 j = strlen(newp);
Alex Deymo486467e2017-12-19 19:04:07 +0100104 for(i = 0; i<j; i++)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700105 if(newp[i] == '?')
106 newp[i] = '\x09';
107
108 /* ... and finally unescape */
Elliott Hughescee03382017-06-23 12:17:18 -0700109 result = Curl_urldecode(data, newp, 0, &sel, &len, FALSE);
Elliott Hughes82be86d2017-09-20 17:00:17 -0700110 if(result)
111 return result;
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700112 sel_org = sel;
113 }
114
115 /* We use Curl_write instead of Curl_sendf to make sure the entire buffer is
116 sent, which could be sizeable with long selectors. */
Alex Deymod15eaac2016-06-28 14:49:26 -0700117 k = curlx_uztosz(len);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700118
119 for(;;) {
120 result = Curl_write(conn, sockfd, sel, k, &amount);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700121 if(!result) { /* Which may not have written it all! */
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700122 result = Curl_client_write(conn, CLIENTWRITE_HEADER, sel, amount);
Elliott Hughescee03382017-06-23 12:17:18 -0700123 if(result)
124 break;
125
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700126 k -= amount;
127 sel += amount;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700128 if(k < 1)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700129 break; /* but it did write it all */
130 }
Elliott Hughescee03382017-06-23 12:17:18 -0700131 else
132 break;
133
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700134 /* Don't busyloop. The entire loop thing is a work-around as it causes a
135 BLOCKING behavior which is a NO-NO. This function should rather be
136 split up in a do and a doing piece where the pieces that aren't
137 possible to send now will be sent in the doing function repeatedly
138 until the entire request is sent.
139
140 Wait a while for the socket to be writable. Note that this doesn't
141 acknowledge the timeout.
142 */
Elliott Hughescee03382017-06-23 12:17:18 -0700143 if(SOCKET_WRITABLE(sockfd, 100) < 0) {
144 result = CURLE_SEND_ERROR;
145 break;
146 }
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700147 }
148
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700149 free(sel_org);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700150
Elliott Hughescee03382017-06-23 12:17:18 -0700151 if(!result)
152 /* We can use Curl_sendf to send the terminal \r\n relatively safely and
153 save allocing another string/doing another _write loop. */
154 result = Curl_sendf(sockfd, conn, "\r\n");
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700155 if(result) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700156 failf(data, "Failed sending Gopher request");
157 return result;
158 }
159 result = Curl_client_write(conn, CLIENTWRITE_HEADER, (char *)"\r\n", 2);
160 if(result)
161 return result;
162
163 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
164 -1, NULL); /* no upload */
165 return CURLE_OK;
166}
167#endif /*CURL_DISABLE_GOPHER*/