Git init
[external/curl.git] / lib / gopher.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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.
13  *
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
23 #include "setup.h"
24
25 #ifndef CURL_DISABLE_GOPHER
26
27 /* -- WIN32 approved -- */
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <ctype.h>
33
34 #ifdef WIN32
35 #include <time.h>
36 #include <io.h>
37 #else
38 #ifdef HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
40 #endif
41 #include <netinet/in.h>
42 #ifdef HAVE_SYS_TIME_H
43 #include <sys/time.h>
44 #endif
45 #ifdef HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif
48 #include <netdb.h>
49 #ifdef HAVE_ARPA_INET_H
50 #include <arpa/inet.h>
51 #endif
52 #ifdef HAVE_NET_IF_H
53 #include <net/if.h>
54 #endif
55 #ifdef HAVE_SYS_IOCTL_H
56 #include <sys/ioctl.h>
57 #endif
58
59 #ifdef HAVE_SYS_PARAM_H
60 #include <sys/param.h>
61 #endif
62
63 #ifdef HAVE_SYS_SELECT_H
64 #include <sys/select.h>
65 #endif
66
67
68 #endif
69
70 #include "urldata.h"
71 #include <curl/curl.h>
72 #include "transfer.h"
73 #include "sendf.h"
74
75 #include "progress.h"
76 #include "strequal.h"
77 #include "gopher.h"
78 #include "rawstr.h"
79 #include "select.h"
80 #include "url.h"
81 #include "warnless.h"
82
83 #define _MPRINTF_REPLACE /* use our functions only */
84 #include <curl/mprintf.h>
85
86 /* The last #include file should be: */
87 #include "memdebug.h"
88
89
90 /*
91  * Forward declarations.
92  */
93
94 static CURLcode gopher_do(struct connectdata *conn, bool *done);
95
96 /*
97  * Gopher protocol handler.
98  * This is also a nice simple template to build off for simple
99  * connect-command-download protocols.
100  */
101
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 */
117 };
118
119 static CURLcode gopher_do(struct connectdata *conn, bool *done)
120 {
121   CURLcode result=CURLE_OK;
122   struct SessionHandle *data=conn->data;
123   curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
124
125   curl_off_t *bytecount = &data->req.bytecount;
126   char *path = data->state.path;
127   char *sel;
128   char *sel_org = NULL;
129   ssize_t amount, k;
130
131   *done = TRUE; /* unconditionally */
132
133   /* Create selector. Degenerate cases: / and /1 => convert to "" */
134   if (strlen(path) <= 2)
135     sel = (char *)"";
136   else {
137     char *newp;
138     size_t j, i;
139     int len;
140
141     /* Otherwise, drop / and the first character (i.e., item type) ... */
142     newp = path;
143     newp+=2;
144
145     /* ... then turn ? into TAB for search servers, Veronica, etc. ... */
146     j = strlen(newp);
147     for(i=0; i<j; i++)
148       if(newp[i] == '?')
149         newp[i] = '\x09';
150
151     /* ... and finally unescape */
152     sel = curl_easy_unescape(data, newp, 0, &len);
153     if (!sel)
154       return CURLE_OUT_OF_MEMORY;
155     sel_org = sel;
156   }
157
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));
161
162   for(;;) {
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);
166       if(result) {
167         Curl_safefree(sel_org);
168         return result;
169       }
170       k -= amount;
171       sel += amount;
172       if (k < 1)
173         break; /* but it did write it all */
174     }
175     else {
176       failf(data, "Failed sending Gopher request");
177       Curl_safefree(sel_org);
178       return result;
179     }
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.
185
186        Wait a while for the socket to be writable. Note that this doesn't
187        acknowledge the timeout.
188     */
189     Curl_socket_ready(CURL_SOCKET_BAD, sockfd, 100);
190   }
191
192   Curl_safefree(sel_org);
193
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");
199     return result;
200   }
201   result = Curl_client_write(conn, CLIENTWRITE_HEADER, (char *)"\r\n", 2);
202   if(result)
203     return result;
204
205   Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
206                       -1, NULL); /* no upload */
207   return CURLE_OK;
208 }
209 #endif /*CURL_DISABLE_GOPHER*/