1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #ifndef CURL_DISABLE_GOPHER
28 #include <curl/curl.h>
37 #include "curl_memory.h"
38 /* The last #include file should be: */
42 * Forward declarations.
45 static CURLcode gopher_do(struct connectdata *conn, bool *done);
48 * Gopher protocol handler.
49 * This is also a nice simple template to build off for simple
50 * connect-command-download protocols.
53 const struct Curl_handler Curl_handler_gopher = {
54 "GOPHER", /* scheme */
55 ZERO_NULL, /* setup_connection */
56 gopher_do, /* do_it */
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 */
64 ZERO_NULL, /* domore_getsock */
65 ZERO_NULL, /* perform_getsock */
66 ZERO_NULL, /* disconnect */
67 ZERO_NULL, /* readwrite */
68 PORT_GOPHER, /* defport */
69 CURLPROTO_GOPHER, /* protocol */
70 PROTOPT_NONE /* flags */
73 static CURLcode gopher_do(struct connectdata *conn, bool *done)
75 CURLcode result=CURLE_OK;
76 struct Curl_easy *data=conn->data;
77 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
79 curl_off_t *bytecount = &data->req.bytecount;
80 char *path = data->state.path;
86 *done = TRUE; /* unconditionally */
88 /* Create selector. Degenerate cases: / and /1 => convert to "" */
89 if(strlen(path) <= 2) {
91 len = (int)strlen(sel);
97 /* Otherwise, drop / and the first character (i.e., item type) ... */
101 /* ... then turn ? into TAB for search servers, Veronica, etc. ... */
107 /* ... and finally unescape */
108 result = Curl_urldecode(data, newp, 0, &sel, &len, FALSE);
110 return CURLE_OUT_OF_MEMORY;
114 /* We use Curl_write instead of Curl_sendf to make sure the entire buffer is
115 sent, which could be sizeable with long selectors. */
116 k = curlx_uztosz(len);
119 result = Curl_write(conn, sockfd, sel, k, &amount);
120 if(!result) { /* Which may not have written it all! */
121 result = Curl_client_write(conn, CLIENTWRITE_HEADER, sel, amount);
128 break; /* but it did write it all */
133 /* Don't busyloop. The entire loop thing is a work-around as it causes a
134 BLOCKING behavior which is a NO-NO. This function should rather be
135 split up in a do and a doing piece where the pieces that aren't
136 possible to send now will be sent in the doing function repeatedly
137 until the entire request is sent.
139 Wait a while for the socket to be writable. Note that this doesn't
140 acknowledge the timeout.
142 if(SOCKET_WRITABLE(sockfd, 100) < 0) {
143 result = CURLE_SEND_ERROR;
151 /* We can use Curl_sendf to send the terminal \r\n relatively safely and
152 save allocing another string/doing another _write loop. */
153 result = Curl_sendf(sockfd, conn, "\r\n");
155 failf(data, "Failed sending Gopher request");
158 result = Curl_client_write(conn, CLIENTWRITE_HEADER, (char *)"\r\n", 2);
162 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
163 -1, NULL); /* no upload */
166 #endif /*CURL_DISABLE_GOPHER*/