[164] | 1 | /***************************************************************************
|
---|
| 2 | * _ _ ____ _
|
---|
| 3 | * Project ___| | | | _ \| |
|
---|
| 4 | * / __| | | | |_) | |
|
---|
| 5 | * | (__| |_| | _ <| |___
|
---|
| 6 | * \___|\___/|_| \_\_____|
|
---|
| 7 | *
|
---|
| 8 | * Copyright (C) 1998 - 2015, 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 |
|
---|
| 32 | #include "progress.h"
|
---|
| 33 | #include "strequal.h"
|
---|
| 34 | #include "gopher.h"
|
---|
| 35 | #include "rawstr.h"
|
---|
| 36 | #include "select.h"
|
---|
| 37 | #include "url.h"
|
---|
| 38 | #include "warnless.h"
|
---|
| 39 | #include "curl_memory.h"
|
---|
| 40 | /* The last #include file should be: */
|
---|
| 41 | #include "memdebug.h"
|
---|
| 42 |
|
---|
| 43 | /*
|
---|
| 44 | * Forward declarations.
|
---|
| 45 | */
|
---|
| 46 |
|
---|
| 47 | static 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 |
|
---|
| 55 | const 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 | PORT_GOPHER, /* defport */
|
---|
| 71 | CURLPROTO_GOPHER, /* protocol */
|
---|
| 72 | PROTOPT_NONE /* flags */
|
---|
| 73 | };
|
---|
| 74 |
|
---|
| 75 | static CURLcode gopher_do(struct connectdata *conn, bool *done)
|
---|
| 76 | {
|
---|
| 77 | CURLcode result=CURLE_OK;
|
---|
| 78 | struct SessionHandle *data=conn->data;
|
---|
| 79 | curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
|
---|
| 80 |
|
---|
| 81 | curl_off_t *bytecount = &data->req.bytecount;
|
---|
| 82 | char *path = data->state.path;
|
---|
| 83 | char *sel;
|
---|
| 84 | char *sel_org = NULL;
|
---|
| 85 | ssize_t amount, k;
|
---|
| 86 | int len;
|
---|
| 87 |
|
---|
| 88 | *done = TRUE; /* unconditionally */
|
---|
| 89 |
|
---|
| 90 | /* Create selector. Degenerate cases: / and /1 => convert to "" */
|
---|
| 91 | if(strlen(path) <= 2) {
|
---|
| 92 | sel = (char *)"";
|
---|
| 93 | len = (int)strlen(sel);
|
---|
| 94 | }
|
---|
| 95 | else {
|
---|
| 96 | char *newp;
|
---|
| 97 | size_t j, i;
|
---|
| 98 |
|
---|
| 99 | /* Otherwise, drop / and the first character (i.e., item type) ... */
|
---|
| 100 | newp = path;
|
---|
| 101 | newp+=2;
|
---|
| 102 |
|
---|
| 103 | /* ... then turn ? into TAB for search servers, Veronica, etc. ... */
|
---|
| 104 | j = strlen(newp);
|
---|
| 105 | for(i=0; i<j; i++)
|
---|
| 106 | if(newp[i] == '?')
|
---|
| 107 | newp[i] = '\x09';
|
---|
| 108 |
|
---|
| 109 | /* ... and finally unescape */
|
---|
| 110 | sel = curl_easy_unescape(data, newp, 0, &len);
|
---|
| 111 | if(!sel)
|
---|
| 112 | return CURLE_OUT_OF_MEMORY;
|
---|
| 113 | sel_org = sel;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | /* We use Curl_write instead of Curl_sendf to make sure the entire buffer is
|
---|
| 117 | sent, which could be sizeable with long selectors. */
|
---|
| 118 | k = curlx_uztosz(len);
|
---|
| 119 |
|
---|
| 120 | for(;;) {
|
---|
| 121 | result = Curl_write(conn, sockfd, sel, k, &amount);
|
---|
| 122 | if(!result) { /* Which may not have written it all! */
|
---|
| 123 | result = Curl_client_write(conn, CLIENTWRITE_HEADER, sel, amount);
|
---|
| 124 | if(result) {
|
---|
| 125 | free(sel_org);
|
---|
| 126 | return result;
|
---|
| 127 | }
|
---|
| 128 | k -= amount;
|
---|
| 129 | sel += amount;
|
---|
| 130 | if(k < 1)
|
---|
| 131 | break; /* but it did write it all */
|
---|
| 132 | }
|
---|
| 133 | else {
|
---|
| 134 | failf(data, "Failed sending Gopher request");
|
---|
| 135 | free(sel_org);
|
---|
| 136 | return result;
|
---|
| 137 | }
|
---|
| 138 | /* Don't busyloop. The entire loop thing is a work-around as it causes a
|
---|
| 139 | BLOCKING behavior which is a NO-NO. This function should rather be
|
---|
| 140 | split up in a do and a doing piece where the pieces that aren't
|
---|
| 141 | possible to send now will be sent in the doing function repeatedly
|
---|
| 142 | until the entire request is sent.
|
---|
| 143 |
|
---|
| 144 | Wait a while for the socket to be writable. Note that this doesn't
|
---|
| 145 | acknowledge the timeout.
|
---|
| 146 | */
|
---|
| 147 | Curl_socket_ready(CURL_SOCKET_BAD, sockfd, 100);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | free(sel_org);
|
---|
| 151 |
|
---|
| 152 | /* We can use Curl_sendf to send the terminal \r\n relatively safely and
|
---|
| 153 | save allocing another string/doing another _write loop. */
|
---|
| 154 | result = Curl_sendf(sockfd, conn, "\r\n");
|
---|
| 155 | if(result) {
|
---|
| 156 | failf(data, "Failed sending Gopher request");
|
---|
| 157 | return result;
|
---|
| 158 | }
|
---|
| 159 | result = Curl_client_write(conn, CLIENTWRITE_HEADER, (char *)"\r\n", 2);
|
---|
| 160 | if(result)
|
---|
| 161 | return result;
|
---|
| 162 |
|
---|
| 163 | Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
|
---|
| 164 | -1, NULL); /* no upload */
|
---|
| 165 | return CURLE_OK;
|
---|
| 166 | }
|
---|
| 167 | #endif /*CURL_DISABLE_GOPHER*/
|
---|