Coverage Report

Created: 2024-05-21 06:52

/src/curl/lib/gopher.c
Line
Count
Source (jump to first uncovered line)
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) 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.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
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
25
#include "curl_setup.h"
26
27
#ifndef CURL_DISABLE_GOPHER
28
29
#include "urldata.h"
30
#include <curl/curl.h>
31
#include "transfer.h"
32
#include "sendf.h"
33
#include "cfilters.h"
34
#include "connect.h"
35
#include "progress.h"
36
#include "gopher.h"
37
#include "select.h"
38
#include "strdup.h"
39
#include "vtls/vtls.h"
40
#include "url.h"
41
#include "escape.h"
42
#include "warnless.h"
43
#include "curl_printf.h"
44
#include "curl_memory.h"
45
/* The last #include file should be: */
46
#include "memdebug.h"
47
48
/*
49
 * Forward declarations.
50
 */
51
52
static CURLcode gopher_do(struct Curl_easy *data, bool *done);
53
#ifdef USE_SSL
54
static CURLcode gopher_connect(struct Curl_easy *data, bool *done);
55
static CURLcode gopher_connecting(struct Curl_easy *data, bool *done);
56
#endif
57
58
/*
59
 * Gopher protocol handler.
60
 * This is also a nice simple template to build off for simple
61
 * connect-command-download protocols.
62
 */
63
64
const struct Curl_handler Curl_handler_gopher = {
65
  "gopher",                             /* scheme */
66
  ZERO_NULL,                            /* setup_connection */
67
  gopher_do,                            /* do_it */
68
  ZERO_NULL,                            /* done */
69
  ZERO_NULL,                            /* do_more */
70
  ZERO_NULL,                            /* connect_it */
71
  ZERO_NULL,                            /* connecting */
72
  ZERO_NULL,                            /* doing */
73
  ZERO_NULL,                            /* proto_getsock */
74
  ZERO_NULL,                            /* doing_getsock */
75
  ZERO_NULL,                            /* domore_getsock */
76
  ZERO_NULL,                            /* perform_getsock */
77
  ZERO_NULL,                            /* disconnect */
78
  ZERO_NULL,                            /* write_resp */
79
  ZERO_NULL,                            /* write_resp_hd */
80
  ZERO_NULL,                            /* connection_check */
81
  ZERO_NULL,                            /* attach connection */
82
  PORT_GOPHER,                          /* defport */
83
  CURLPROTO_GOPHER,                     /* protocol */
84
  CURLPROTO_GOPHER,                     /* family */
85
  PROTOPT_NONE                          /* flags */
86
};
87
88
#ifdef USE_SSL
89
const struct Curl_handler Curl_handler_gophers = {
90
  "gophers",                            /* scheme */
91
  ZERO_NULL,                            /* setup_connection */
92
  gopher_do,                            /* do_it */
93
  ZERO_NULL,                            /* done */
94
  ZERO_NULL,                            /* do_more */
95
  gopher_connect,                       /* connect_it */
96
  gopher_connecting,                    /* connecting */
97
  ZERO_NULL,                            /* doing */
98
  ZERO_NULL,                            /* proto_getsock */
99
  ZERO_NULL,                            /* doing_getsock */
100
  ZERO_NULL,                            /* domore_getsock */
101
  ZERO_NULL,                            /* perform_getsock */
102
  ZERO_NULL,                            /* disconnect */
103
  ZERO_NULL,                            /* write_resp */
104
  ZERO_NULL,                            /* write_resp_hd */
105
  ZERO_NULL,                            /* connection_check */
106
  ZERO_NULL,                            /* attach connection */
107
  PORT_GOPHER,                          /* defport */
108
  CURLPROTO_GOPHERS,                    /* protocol */
109
  CURLPROTO_GOPHER,                     /* family */
110
  PROTOPT_SSL                           /* flags */
111
};
112
113
static CURLcode gopher_connect(struct Curl_easy *data, bool *done)
114
0
{
115
0
  (void)data;
116
0
  (void)done;
117
0
  return CURLE_OK;
118
0
}
119
120
static CURLcode gopher_connecting(struct Curl_easy *data, bool *done)
121
0
{
122
0
  struct connectdata *conn = data->conn;
123
0
  CURLcode result;
124
125
0
  result = Curl_conn_connect(data, FIRSTSOCKET, TRUE, done);
126
0
  if(result)
127
0
    connclose(conn, "Failed TLS connection");
128
0
  *done = TRUE;
129
0
  return result;
130
0
}
131
#endif
132
133
static CURLcode gopher_do(struct Curl_easy *data, bool *done)
134
605
{
135
605
  CURLcode result = CURLE_OK;
136
605
  struct connectdata *conn = data->conn;
137
605
  curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
138
605
  char *gopherpath;
139
605
  char *path = data->state.up.path;
140
605
  char *query = data->state.up.query;
141
605
  char *sel = NULL;
142
605
  char *sel_org = NULL;
143
605
  timediff_t timeout_ms;
144
605
  ssize_t k;
145
605
  size_t amount, len;
146
605
  int what;
147
148
605
  *done = TRUE; /* unconditionally */
149
150
  /* path is guaranteed non-NULL */
151
605
  DEBUGASSERT(path);
152
153
605
  if(query)
154
118
    gopherpath = aprintf("%s?%s", path, query);
155
487
  else
156
487
    gopherpath = strdup(path);
157
158
605
  if(!gopherpath)
159
0
    return CURLE_OUT_OF_MEMORY;
160
161
  /* Create selector. Degenerate cases: / and /1 => convert to "" */
162
605
  if(strlen(gopherpath) <= 2) {
163
351
    sel = (char *)"";
164
351
    len = strlen(sel);
165
351
    free(gopherpath);
166
351
  }
167
254
  else {
168
254
    char *newp;
169
170
    /* Otherwise, drop / and the first character (i.e., item type) ... */
171
254
    newp = gopherpath;
172
254
    newp += 2;
173
174
    /* ... and finally unescape */
175
254
    result = Curl_urldecode(newp, 0, &sel, &len, REJECT_ZERO);
176
254
    free(gopherpath);
177
254
    if(result)
178
2
      return result;
179
252
    sel_org = sel;
180
252
  }
181
182
603
  k = curlx_uztosz(len);
183
184
603
  for(;;) {
185
    /* Break out of the loop if the selector is empty because OpenSSL and/or
186
       LibreSSL fail with errno 0 if this is the case. */
187
603
    if(strlen(sel) < 1)
188
351
      break;
189
190
252
    result = Curl_xfer_send(data, sel, k, &amount);
191
252
    if(!result) { /* Which may not have written it all! */
192
252
      result = Curl_client_write(data, CLIENTWRITE_HEADER, sel, amount);
193
252
      if(result)
194
0
        break;
195
196
252
      k -= amount;
197
252
      sel += amount;
198
252
      if(k < 1)
199
221
        break; /* but it did write it all */
200
252
    }
201
0
    else
202
0
      break;
203
204
31
    timeout_ms = Curl_timeleft(data, NULL, FALSE);
205
31
    if(timeout_ms < 0) {
206
0
      result = CURLE_OPERATION_TIMEDOUT;
207
0
      break;
208
0
    }
209
31
    if(!timeout_ms)
210
0
      timeout_ms = TIMEDIFF_T_MAX;
211
212
    /* Don't busyloop. The entire loop thing is a work-around as it causes a
213
       BLOCKING behavior which is a NO-NO. This function should rather be
214
       split up in a do and a doing piece where the pieces that aren't
215
       possible to send now will be sent in the doing function repeatedly
216
       until the entire request is sent.
217
    */
218
31
    what = SOCKET_WRITABLE(sockfd, timeout_ms);
219
31
    if(what < 0) {
220
0
      result = CURLE_SEND_ERROR;
221
0
      break;
222
0
    }
223
31
    else if(!what) {
224
31
      result = CURLE_OPERATION_TIMEDOUT;
225
31
      break;
226
31
    }
227
31
  }
228
229
603
  free(sel_org);
230
231
603
  if(!result)
232
572
    result = Curl_xfer_send(data, "\r\n", 2, &amount);
233
603
  if(result) {
234
31
    failf(data, "Failed sending Gopher request");
235
31
    return result;
236
31
  }
237
572
  result = Curl_client_write(data, CLIENTWRITE_HEADER, (char *)"\r\n", 2);
238
572
  if(result)
239
0
    return result;
240
241
572
  Curl_xfer_setup(data, FIRSTSOCKET, -1, FALSE, -1);
242
572
  return CURLE_OK;
243
572
}
244
#endif /* CURL_DISABLE_GOPHER */