Imported Upstream version 7.53.1
[platform/upstream/curl.git] / lib / gopher.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2016, 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 https://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 "curl_setup.h"
24
25 #ifndef CURL_DISABLE_GOPHER
26
27 #include "urldata.h"
28 #include <curl/curl.h>
29 #include "transfer.h"
30 #include "sendf.h"
31 #include "progress.h"
32 #include "gopher.h"
33 #include "select.h"
34 #include "url.h"
35 #include "escape.h"
36 #include "warnless.h"
37 #include "curl_memory.h"
38 /* The last #include file should be: */
39 #include "memdebug.h"
40
41 /*
42  * Forward declarations.
43  */
44
45 static CURLcode gopher_do(struct connectdata *conn, bool *done);
46
47 /*
48  * Gopher protocol handler.
49  * This is also a nice simple template to build off for simple
50  * connect-command-download protocols.
51  */
52
53 const struct Curl_handler Curl_handler_gopher = {
54   "GOPHER",                             /* scheme */
55   ZERO_NULL,                            /* setup_connection */
56   gopher_do,                            /* do_it */
57   ZERO_NULL,                            /* done */
58   ZERO_NULL,                            /* do_more */
59   ZERO_NULL,                            /* connect_it */
60   ZERO_NULL,                            /* connecting */
61   ZERO_NULL,                            /* doing */
62   ZERO_NULL,                            /* proto_getsock */
63   ZERO_NULL,                            /* doing_getsock */
64   ZERO_NULL,                            /* domore_getsock */
65   ZERO_NULL,                            /* perform_getsock */
66   ZERO_NULL,                            /* disconnect */
67   ZERO_NULL,                            /* readwrite */
68   PORT_GOPHER,                          /* defport */
69   CURLPROTO_GOPHER,                     /* protocol */
70   PROTOPT_NONE                          /* flags */
71 };
72
73 static CURLcode gopher_do(struct connectdata *conn, bool *done)
74 {
75   CURLcode result=CURLE_OK;
76   struct Curl_easy *data=conn->data;
77   curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
78
79   curl_off_t *bytecount = &data->req.bytecount;
80   char *path = data->state.path;
81   char *sel;
82   char *sel_org = NULL;
83   ssize_t amount, k;
84   size_t len;
85
86   *done = TRUE; /* unconditionally */
87
88   /* Create selector. Degenerate cases: / and /1 => convert to "" */
89   if(strlen(path) <= 2) {
90     sel = (char *)"";
91     len = (int)strlen(sel);
92   }
93   else {
94     char *newp;
95     size_t j, i;
96
97     /* Otherwise, drop / and the first character (i.e., item type) ... */
98     newp = path;
99     newp+=2;
100
101     /* ... then turn ? into TAB for search servers, Veronica, etc. ... */
102     j = strlen(newp);
103     for(i=0; i<j; i++)
104       if(newp[i] == '?')
105         newp[i] = '\x09';
106
107     /* ... and finally unescape */
108     result = Curl_urldecode(data, newp, 0, &sel, &len, FALSE);
109     if(!sel)
110       return CURLE_OUT_OF_MEMORY;
111     sel_org = sel;
112   }
113
114   /* We use Curl_write instead of Curl_sendf to make sure the entire buffer is
115      sent, which could be sizeable with long selectors. */
116   k = curlx_uztosz(len);
117
118   for(;;) {
119     result = Curl_write(conn, sockfd, sel, k, &amount);
120     if(!result) { /* Which may not have written it all! */
121       result = Curl_client_write(conn, CLIENTWRITE_HEADER, sel, amount);
122       if(result)
123         break;
124
125       k -= amount;
126       sel += amount;
127       if(k < 1)
128         break; /* but it did write it all */
129     }
130     else
131       break;
132
133     /* Don't busyloop. The entire loop thing is a work-around as it causes a
134        BLOCKING behavior which is a NO-NO. This function should rather be
135        split up in a do and a doing piece where the pieces that aren't
136        possible to send now will be sent in the doing function repeatedly
137        until the entire request is sent.
138
139        Wait a while for the socket to be writable. Note that this doesn't
140        acknowledge the timeout.
141     */
142     if(SOCKET_WRITABLE(sockfd, 100) < 0) {
143       result = CURLE_SEND_ERROR;
144       break;
145     }
146   }
147
148   free(sel_org);
149
150   if(!result)
151     /* We can use Curl_sendf to send the terminal \r\n relatively safely and
152        save allocing another string/doing another _write loop. */
153     result = Curl_sendf(sockfd, conn, "\r\n");
154   if(result) {
155     failf(data, "Failed sending Gopher request");
156     return result;
157   }
158   result = Curl_client_write(conn, CLIENTWRITE_HEADER, (char *)"\r\n", 2);
159   if(result)
160     return result;
161
162   Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
163                       -1, NULL); /* no upload */
164   return CURLE_OK;
165 }
166 #endif /*CURL_DISABLE_GOPHER*/