Gopher using Curl_write; test suite (4 tests)
[platform/upstream/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
80 #define _MPRINTF_REPLACE /* use our functions only */
81 #include <curl/mprintf.h>
82
83 /* The last #include file should be: */
84 #include "memdebug.h"
85
86
87 /*
88  * Forward declarations.
89  */
90
91 static CURLcode gopher_do(struct connectdata *conn, bool *done);
92
93 /*
94  * Gopher protocol handler.
95  * This is also a nice simple template to build off for simple
96  * connect-command-download protocols.
97  */
98
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 */
114 };
115
116 static CURLcode gopher_do(struct connectdata *conn, bool *done)
117 {
118   CURLcode result=CURLE_OK;
119   struct SessionHandle *data=conn->data;
120   curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
121
122   curl_off_t *bytecount = &data->req.bytecount;
123   char *path = data->state.path;
124   char *sel;
125   ssize_t amount, k;
126
127   *done = TRUE; /* unconditionally */
128
129   /* Create selector. Degenerate cases: / and /1 => convert to "" */
130   if (strlen(path) <= 2)
131     sel = (char *)"";
132   else {
133     char *newp;
134     int i, j, len;
135
136     /* Otherwise, drop / and the first character (i.e., item type) ... */
137     newp = path;
138     newp+=2;
139
140     /* ... then turn ? into TAB for search servers, Veronica, etc. ... */
141     j = strlen(newp);
142     if (j)
143       for(i=0; i<j; i++)
144         newp[i] = ((newp[i] == '?') ? '\x09' : newp[i]);
145
146     /* ... and finally unescape */
147     sel = curl_easy_unescape(data, newp, 0, &len);
148     if (!sel)
149       return CURLE_OUT_OF_MEMORY;
150   }
151
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. */
154   k = strlen(sel);
155   for(;;) {
156     result = Curl_write(conn, sockfd, sel, k, &amount);
157     if (CURLE_OK == result) { /* Which may not have written it all! */
158       k -= amount;
159       sel += amount;
160       if (k < 1)
161         break; /* but it did write it all */
162     } else {
163       failf(data, "Failed sending Gopher request");
164       return result;
165     }
166   }
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");
172     return result;
173   }
174
175   Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
176                       -1, NULL); /* no upload */
177   return CURLE_OK;
178 }
179 #endif /*CURL_DISABLE_GOPHER*/