1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, 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 #ifdef HAVE_SYS_SOCKET_H
28 #include <sys/socket.h>
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
39 #ifdef HAVE_ARPA_INET_H
40 #include <arpa/inet.h>
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
49 #ifdef HAVE_SYS_PARAM_H
50 #include <sys/param.h>
53 #ifdef HAVE_SYS_SELECT_H
54 #include <sys/select.h>
58 #include <curl/curl.h>
70 #define _MPRINTF_REPLACE /* use our functions only */
71 #include <curl/mprintf.h>
73 /* The last #include file should be: */
78 * Forward declarations.
81 static CURLcode gopher_do(struct connectdata *conn, bool *done);
84 * Gopher protocol handler.
85 * This is also a nice simple template to build off for simple
86 * connect-command-download protocols.
89 const struct Curl_handler Curl_handler_gopher = {
90 "GOPHER", /* scheme */
91 ZERO_NULL, /* setup_connection */
92 gopher_do, /* do_it */
94 ZERO_NULL, /* do_more */
95 ZERO_NULL, /* connect_it */
96 ZERO_NULL, /* connecting */
97 ZERO_NULL, /* doing */
98 ZERO_NULL, /* proto_getsock */
99 ZERO_NULL, /* doing_getsock */
100 ZERO_NULL, /* domore_getsock */
101 ZERO_NULL, /* perform_getsock */
102 ZERO_NULL, /* disconnect */
103 ZERO_NULL, /* readwrite */
104 PORT_GOPHER, /* defport */
105 CURLPROTO_GOPHER, /* protocol */
106 PROTOPT_NONE /* flags */
109 static CURLcode gopher_do(struct connectdata *conn, bool *done)
111 CURLcode result=CURLE_OK;
112 struct SessionHandle *data=conn->data;
113 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
115 curl_off_t *bytecount = &data->req.bytecount;
116 char *path = data->state.path;
118 char *sel_org = NULL;
121 *done = TRUE; /* unconditionally */
123 /* Create selector. Degenerate cases: / and /1 => convert to "" */
124 if(strlen(path) <= 2)
131 /* Otherwise, drop / and the first character (i.e., item type) ... */
135 /* ... then turn ? into TAB for search servers, Veronica, etc. ... */
141 /* ... and finally unescape */
142 sel = curl_easy_unescape(data, newp, 0, &len);
144 return CURLE_OUT_OF_MEMORY;
148 /* We use Curl_write instead of Curl_sendf to make sure the entire buffer is
149 sent, which could be sizeable with long selectors. */
150 k = curlx_uztosz(strlen(sel));
153 result = Curl_write(conn, sockfd, sel, k, &amount);
154 if(CURLE_OK == result) { /* Which may not have written it all! */
155 result = Curl_client_write(conn, CLIENTWRITE_HEADER, sel, amount);
157 Curl_safefree(sel_org);
163 break; /* but it did write it all */
166 failf(data, "Failed sending Gopher request");
167 Curl_safefree(sel_org);
170 /* Don't busyloop. The entire loop thing is a work-around as it causes a
171 BLOCKING behavior which is a NO-NO. This function should rather be
172 split up in a do and a doing piece where the pieces that aren't
173 possible to send now will be sent in the doing function repeatedly
174 until the entire request is sent.
176 Wait a while for the socket to be writable. Note that this doesn't
177 acknowledge the timeout.
179 Curl_socket_ready(CURL_SOCKET_BAD, sockfd, 100);
182 Curl_safefree(sel_org);
184 /* We can use Curl_sendf to send the terminal \r\n relatively safely and
185 save allocing another string/doing another _write loop. */
186 result = Curl_sendf(sockfd, conn, "\r\n");
187 if(result != CURLE_OK) {
188 failf(data, "Failed sending Gopher request");
191 result = Curl_client_write(conn, CLIENTWRITE_HEADER, (char *)"\r\n", 2);
195 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
196 -1, NULL); /* no upload */
199 #endif /*CURL_DISABLE_GOPHER*/