VirtualBox

source: vbox/trunk/src/libs/curl-7.64.0/lib/gopher.c@ 94601

Last change on this file since 94601 was 85671, checked in by vboxsync, 4 years ago

Export out internal curl copy to make it a lot simpler to build VBox (OSE) on Windows. bugref:9814

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette