1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2010, 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 http://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 ***************************************************************************/
25 #ifndef CURL_DISABLE_GOPHER
27 /* -- WIN32 approved -- */
38 #ifdef HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #ifdef HAVE_SYS_TIME_H
49 #ifdef HAVE_ARPA_INET_H
50 #include <arpa/inet.h>
55 #ifdef HAVE_SYS_IOCTL_H
56 #include <sys/ioctl.h>
59 #ifdef HAVE_SYS_PARAM_H
60 #include <sys/param.h>
63 #ifdef HAVE_SYS_SELECT_H
64 #include <sys/select.h>
71 #include <curl/curl.h>
83 #define _MPRINTF_REPLACE /* use our functions only */
84 #include <curl/mprintf.h>
86 /* The last #include file should be: */
91 * Forward declarations.
94 static CURLcode gopher_do(struct connectdata *conn, bool *done);
97 * Gopher protocol handler.
98 * This is also a nice simple template to build off for simple
99 * connect-command-download protocols.
102 const struct Curl_handler Curl_handler_gopher = {
103 "GOPHER", /* scheme */
104 ZERO_NULL, /* setup_connection */
105 gopher_do, /* do_it */
106 ZERO_NULL, /* done */
107 ZERO_NULL, /* do_more */
108 ZERO_NULL, /* connect_it */
109 ZERO_NULL, /* connecting */
110 ZERO_NULL, /* doing */
111 ZERO_NULL, /* proto_getsock */
112 ZERO_NULL, /* doing_getsock */
113 ZERO_NULL, /* perform_getsock */
114 ZERO_NULL, /* disconnect */
115 PORT_GOPHER, /* defport */
116 PROT_GOPHER /* protocol */
119 static CURLcode gopher_do(struct connectdata *conn, bool *done)
121 CURLcode result=CURLE_OK;
122 struct SessionHandle *data=conn->data;
123 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
125 curl_off_t *bytecount = &data->req.bytecount;
126 char *path = data->state.path;
128 char *sel_org = NULL;
131 *done = TRUE; /* unconditionally */
133 /* Create selector. Degenerate cases: / and /1 => convert to "" */
134 if (strlen(path) <= 2)
141 /* Otherwise, drop / and the first character (i.e., item type) ... */
145 /* ... then turn ? into TAB for search servers, Veronica, etc. ... */
151 /* ... and finally unescape */
152 sel = curl_easy_unescape(data, newp, 0, &len);
154 return CURLE_OUT_OF_MEMORY;
158 /* We use Curl_write instead of Curl_sendf to make sure the entire buffer is
159 sent, which could be sizeable with long selectors. */
160 k = curlx_uztosz(strlen(sel));
163 result = Curl_write(conn, sockfd, sel, k, &amount);
164 if (CURLE_OK == result) { /* Which may not have written it all! */
165 result = Curl_client_write(conn, CLIENTWRITE_HEADER, sel, amount);
167 Curl_safefree(sel_org);
173 break; /* but it did write it all */
176 failf(data, "Failed sending Gopher request");
177 Curl_safefree(sel_org);
180 /* Don't busyloop. The entire loop thing is a work-around as it causes a
181 BLOCKING behavior which is a NO-NO. This function should rather be
182 split up in a do and a doing piece where the pieces that aren't
183 possible to send now will be sent in the doing function repeatedly
184 until the entire request is sent.
186 Wait a while for the socket to be writable. Note that this doesn't
187 acknowledge the timeout.
189 Curl_socket_ready(CURL_SOCKET_BAD, sockfd, 100);
192 Curl_safefree(sel_org);
194 /* We can use Curl_sendf to send the terminal \r\n relatively safely and
195 save allocing another string/doing another _write loop. */
196 result = Curl_sendf(sockfd, conn, "\r\n");
197 if (result != CURLE_OK) {
198 failf(data, "Failed sending Gopher request");
201 result = Curl_client_write(conn, CLIENTWRITE_HEADER, (char *)"\r\n", 2);
205 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
206 -1, NULL); /* no upload */
209 #endif /*CURL_DISABLE_GOPHER*/