Coverage Report

Created: 2025-06-20 06:58

/src/PROJ/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 "curlx/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
  ZERO_NULL,                            /* follow */
83
  PORT_GOPHER,                          /* defport */
84
  CURLPROTO_GOPHER,                     /* protocol */
85
  CURLPROTO_GOPHER,                     /* family */
86
  PROTOPT_NONE                          /* flags */
87
};
88
89
#ifdef USE_SSL
90
const struct Curl_handler Curl_handler_gophers = {
91
  "gophers",                            /* scheme */
92
  ZERO_NULL,                            /* setup_connection */
93
  gopher_do,                            /* do_it */
94
  ZERO_NULL,                            /* done */
95
  ZERO_NULL,                            /* do_more */
96
  gopher_connect,                       /* connect_it */
97
  gopher_connecting,                    /* connecting */
98
  ZERO_NULL,                            /* doing */
99
  ZERO_NULL,                            /* proto_getsock */
100
  ZERO_NULL,                            /* doing_getsock */
101
  ZERO_NULL,                            /* domore_getsock */
102
  ZERO_NULL,                            /* perform_getsock */
103
  ZERO_NULL,                            /* disconnect */
104
  ZERO_NULL,                            /* write_resp */
105
  ZERO_NULL,                            /* write_resp_hd */
106
  ZERO_NULL,                            /* connection_check */
107
  ZERO_NULL,                            /* attach connection */
108
  ZERO_NULL,                            /* follow */
109
  PORT_GOPHER,                          /* defport */
110
  CURLPROTO_GOPHERS,                    /* protocol */
111
  CURLPROTO_GOPHER,                     /* family */
112
  PROTOPT_SSL                           /* flags */
113
};
114
115
static CURLcode gopher_connect(struct Curl_easy *data, bool *done)
116
0
{
117
0
  (void)data;
118
0
  (void)done;
119
0
  return CURLE_OK;
120
0
}
121
122
static CURLcode gopher_connecting(struct Curl_easy *data, bool *done)
123
0
{
124
0
  struct connectdata *conn = data->conn;
125
0
  CURLcode result;
126
127
0
  result = Curl_conn_connect(data, FIRSTSOCKET, TRUE, done);
128
0
  if(result)
129
0
    connclose(conn, "Failed TLS connection");
130
0
  *done = TRUE;
131
0
  return result;
132
0
}
133
#endif
134
135
static CURLcode gopher_do(struct Curl_easy *data, bool *done)
136
0
{
137
0
  CURLcode result = CURLE_OK;
138
0
  struct connectdata *conn = data->conn;
139
0
  curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
140
0
  char *gopherpath;
141
0
  char *path = data->state.up.path;
142
0
  char *query = data->state.up.query;
143
0
  char *sel = NULL;
144
0
  char *sel_org = NULL;
145
0
  timediff_t timeout_ms;
146
0
  ssize_t k;
147
0
  size_t amount, len;
148
0
  int what;
149
150
0
  *done = TRUE; /* unconditionally */
151
152
  /* path is guaranteed non-NULL */
153
0
  DEBUGASSERT(path);
154
155
0
  if(query)
156
0
    gopherpath = aprintf("%s?%s", path, query);
157
0
  else
158
0
    gopherpath = strdup(path);
159
160
0
  if(!gopherpath)
161
0
    return CURLE_OUT_OF_MEMORY;
162
163
  /* Create selector. Degenerate cases: / and /1 => convert to "" */
164
0
  if(strlen(gopherpath) <= 2) {
165
0
    sel = (char *)CURL_UNCONST("");
166
0
    len = strlen(sel);
167
0
    free(gopherpath);
168
0
  }
169
0
  else {
170
0
    char *newp;
171
172
    /* Otherwise, drop / and the first character (i.e., item type) ... */
173
0
    newp = gopherpath;
174
0
    newp += 2;
175
176
    /* ... and finally unescape */
177
0
    result = Curl_urldecode(newp, 0, &sel, &len, REJECT_ZERO);
178
0
    free(gopherpath);
179
0
    if(result)
180
0
      return result;
181
0
    sel_org = sel;
182
0
  }
183
184
0
  k = curlx_uztosz(len);
185
186
0
  for(;;) {
187
    /* Break out of the loop if the selector is empty because OpenSSL and/or
188
       LibreSSL fail with errno 0 if this is the case. */
189
0
    if(strlen(sel) < 1)
190
0
      break;
191
192
0
    result = Curl_xfer_send(data, sel, k, FALSE, &amount);
193
0
    if(!result) { /* Which may not have written it all! */
194
0
      result = Curl_client_write(data, CLIENTWRITE_HEADER, sel, amount);
195
0
      if(result)
196
0
        break;
197
198
0
      k -= amount;
199
0
      sel += amount;
200
0
      if(k < 1)
201
0
        break; /* but it did write it all */
202
0
    }
203
0
    else
204
0
      break;
205
206
0
    timeout_ms = Curl_timeleft(data, NULL, FALSE);
207
0
    if(timeout_ms < 0) {
208
0
      result = CURLE_OPERATION_TIMEDOUT;
209
0
      break;
210
0
    }
211
0
    if(!timeout_ms)
212
0
      timeout_ms = TIMEDIFF_T_MAX;
213
214
    /* Do not busyloop. The entire loop thing is a work-around as it causes a
215
       BLOCKING behavior which is a NO-NO. This function should rather be
216
       split up in a do and a doing piece where the pieces that are not
217
       possible to send now will be sent in the doing function repeatedly
218
       until the entire request is sent.
219
    */
220
0
    what = SOCKET_WRITABLE(sockfd, timeout_ms);
221
0
    if(what < 0) {
222
0
      result = CURLE_SEND_ERROR;
223
0
      break;
224
0
    }
225
0
    else if(!what) {
226
0
      result = CURLE_OPERATION_TIMEDOUT;
227
0
      break;
228
0
    }
229
0
  }
230
231
0
  free(sel_org);
232
233
0
  if(!result)
234
0
    result = Curl_xfer_send(data, "\r\n", 2, FALSE, &amount);
235
0
  if(result) {
236
0
    failf(data, "Failed sending Gopher request");
237
0
    return result;
238
0
  }
239
0
  result = Curl_client_write(data, CLIENTWRITE_HEADER, "\r\n", 2);
240
0
  if(result)
241
0
    return result;
242
243
0
  Curl_xfer_setup1(data, CURL_XFER_RECV, -1, FALSE);
244
0
  return CURLE_OK;
245
0
}
246
#endif /* CURL_DISABLE_GOPHER */