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>
80 #define _MPRINTF_REPLACE /* use our functions only */
81 #include <curl/mprintf.h>
83 /* The last #include file should be: */
88 * Forward declarations.
91 static CURLcode gopher_do(struct connectdata *conn, bool *done);
94 * Gopher protocol handler.
95 * This is also a nice simple template to build off for simple
96 * connect-command-download protocols.
99 const struct Curl_handler Curl_handler_gopher = {
100 "GOPHER", /* scheme */
101 ZERO_NULL, /* setup_connection */
102 gopher_do, /* do_it */
103 ZERO_NULL, /* done */
104 ZERO_NULL, /* do_more */
105 ZERO_NULL, /* connect_it */
106 ZERO_NULL, /* connecting */
107 ZERO_NULL, /* doing */
108 ZERO_NULL, /* proto_getsock */
109 ZERO_NULL, /* doing_getsock */
110 ZERO_NULL, /* perform_getsock */
111 ZERO_NULL, /* disconnect */
112 PORT_GOPHER, /* defport */
113 PROT_GOPHER /* protocol */
116 static CURLcode gopher_do(struct connectdata *conn, bool *done)
118 CURLcode result=CURLE_OK;
119 struct SessionHandle *data=conn->data;
120 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
122 curl_off_t *bytecount = &data->req.bytecount;
123 char *path = data->state.path;
127 *done = TRUE; /* unconditionally */
129 /* Create selector. Degenerate cases: / and /1 => convert to "" */
130 if (strlen(path) <= 2)
136 /* Otherwise, drop / and the first character (i.e., item type) ... */
140 /* ... then turn ? into TAB for search servers, Veronica, etc. ... */
144 newp[i] = ((newp[i] == '?') ? '\x09' : newp[i]);
146 /* ... and finally unescape */
147 sel = curl_easy_unescape(data, newp, 0, &len);
149 return CURLE_OUT_OF_MEMORY;
152 /* We use Curl_write instead of Curl_sendf to make sure the entire buffer
153 is sent, which could be sizeable with long selectors. */
156 result = Curl_write(conn, sockfd, sel, k, &amount);
157 if (CURLE_OK == result) { /* Which may not have written it all! */
161 break; /* but it did write it all */
163 failf(data, "Failed sending Gopher request");
167 /* We can use Curl_sendf to send the terminal \r\n relatively safely and
168 save allocing another string/doing another _write loop. */
169 result = Curl_sendf(sockfd, conn, "\r\n");
170 if (result != CURLE_OK) {
171 failf(data, "Failed sending Gopher request");
175 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
176 -1, NULL); /* no upload */
179 #endif /*CURL_DISABLE_GOPHER*/