Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 8 | * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al. |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 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 |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 12 | * are also available at https://curl.haxx.se/docs/copyright.html. |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 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 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 23 | #include "curl_setup.h" |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 24 | |
| 25 | #ifndef CURL_DISABLE_GOPHER |
| 26 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 27 | #include "urldata.h" |
| 28 | #include <curl/curl.h> |
| 29 | #include "transfer.h" |
| 30 | #include "sendf.h" |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 31 | #include "progress.h" |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 32 | #include "gopher.h" |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 33 | #include "select.h" |
| 34 | #include "url.h" |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 35 | #include "escape.h" |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 36 | #include "warnless.h" |
| 37 | #include "curl_memory.h" |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 38 | /* The last #include file should be: */ |
| 39 | #include "memdebug.h" |
| 40 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 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 */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 64 | ZERO_NULL, /* domore_getsock */ |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 65 | ZERO_NULL, /* perform_getsock */ |
| 66 | ZERO_NULL, /* disconnect */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 67 | ZERO_NULL, /* readwrite */ |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 68 | ZERO_NULL, /* connection_check */ |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 69 | PORT_GOPHER, /* defport */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 70 | CURLPROTO_GOPHER, /* protocol */ |
| 71 | PROTOPT_NONE /* flags */ |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | static CURLcode gopher_do(struct connectdata *conn, bool *done) |
| 75 | { |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 76 | CURLcode result = CURLE_OK; |
| 77 | struct Curl_easy *data = conn->data; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 78 | curl_socket_t sockfd = conn->sock[FIRSTSOCKET]; |
| 79 | |
| 80 | curl_off_t *bytecount = &data->req.bytecount; |
| 81 | char *path = data->state.path; |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 82 | char *sel = NULL; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 83 | char *sel_org = NULL; |
| 84 | ssize_t amount, k; |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 85 | size_t len; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 86 | |
| 87 | *done = TRUE; /* unconditionally */ |
| 88 | |
| 89 | /* Create selector. Degenerate cases: / and /1 => convert to "" */ |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 90 | if(strlen(path) <= 2) { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 91 | sel = (char *)""; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 92 | len = (int)strlen(sel); |
| 93 | } |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 94 | else { |
| 95 | char *newp; |
| 96 | size_t j, i; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 97 | |
| 98 | /* Otherwise, drop / and the first character (i.e., item type) ... */ |
| 99 | newp = path; |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 100 | newp += 2; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 101 | |
| 102 | /* ... then turn ? into TAB for search servers, Veronica, etc. ... */ |
| 103 | j = strlen(newp); |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 104 | for(i = 0; i<j; i++) |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 105 | if(newp[i] == '?') |
| 106 | newp[i] = '\x09'; |
| 107 | |
| 108 | /* ... and finally unescape */ |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 109 | result = Curl_urldecode(data, newp, 0, &sel, &len, FALSE); |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 110 | if(result) |
| 111 | return result; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 112 | sel_org = sel; |
| 113 | } |
| 114 | |
| 115 | /* We use Curl_write instead of Curl_sendf to make sure the entire buffer is |
| 116 | sent, which could be sizeable with long selectors. */ |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 117 | k = curlx_uztosz(len); |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 118 | |
| 119 | for(;;) { |
| 120 | result = Curl_write(conn, sockfd, sel, k, &amount); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 121 | if(!result) { /* Which may not have written it all! */ |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 122 | result = Curl_client_write(conn, CLIENTWRITE_HEADER, sel, amount); |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 123 | if(result) |
| 124 | break; |
| 125 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 126 | k -= amount; |
| 127 | sel += amount; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 128 | if(k < 1) |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 129 | break; /* but it did write it all */ |
| 130 | } |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 131 | else |
| 132 | break; |
| 133 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 134 | /* Don't busyloop. The entire loop thing is a work-around as it causes a |
| 135 | BLOCKING behavior which is a NO-NO. This function should rather be |
| 136 | split up in a do and a doing piece where the pieces that aren't |
| 137 | possible to send now will be sent in the doing function repeatedly |
| 138 | until the entire request is sent. |
| 139 | |
| 140 | Wait a while for the socket to be writable. Note that this doesn't |
| 141 | acknowledge the timeout. |
| 142 | */ |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 143 | if(SOCKET_WRITABLE(sockfd, 100) < 0) { |
| 144 | result = CURLE_SEND_ERROR; |
| 145 | break; |
| 146 | } |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 149 | free(sel_org); |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 150 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 151 | if(!result) |
| 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"); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 155 | if(result) { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 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*/ |