Coverage Report

Created: 2025-10-10 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/gopher.c
Line
Count
Source
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
44
/* The last 2 #include files should be: */
45
#include "curl_memory.h"
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_pollset */
74
  ZERO_NULL,                            /* doing_pollset */
75
  ZERO_NULL,                            /* domore_pollset */
76
  ZERO_NULL,                            /* perform_pollset */
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_pollset */
100
  ZERO_NULL,                            /* doing_pollset */
101
  ZERO_NULL,                            /* domore_pollset */
102
  ZERO_NULL,                            /* perform_pollset */
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
865
{
137
865
  CURLcode result = CURLE_OK;
138
865
  struct connectdata *conn = data->conn;
139
865
  curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
140
865
  char *gopherpath;
141
865
  char *path = data->state.up.path;
142
865
  char *query = data->state.up.query;
143
865
  char *sel = NULL;
144
865
  char *sel_org = NULL;
145
865
  timediff_t timeout_ms;
146
865
  ssize_t k;
147
865
  size_t amount, len;
148
865
  int what;
149
150
865
  *done = TRUE; /* unconditionally */
151
152
  /* path is guaranteed non-NULL */
153
865
  DEBUGASSERT(path);
154
155
865
  if(query)
156
76
    gopherpath = curl_maprintf("%s?%s", path, query);
157
789
  else
158
789
    gopherpath = strdup(path);
159
160
865
  if(!gopherpath)
161
0
    return CURLE_OUT_OF_MEMORY;
162
163
  /* Create selector. Degenerate cases: / and /1 => convert to "" */
164
865
  if(strlen(gopherpath) <= 2) {
165
718
    sel = (char *)CURL_UNCONST("");
166
718
    len = strlen(sel);
167
718
    free(gopherpath);
168
718
  }
169
147
  else {
170
147
    char *newp;
171
172
    /* Otherwise, drop / and the first character (i.e., item type) ... */
173
147
    newp = gopherpath;
174
147
    newp += 2;
175
176
    /* ... and finally unescape */
177
147
    result = Curl_urldecode(newp, 0, &sel, &len, REJECT_ZERO);
178
147
    free(gopherpath);
179
147
    if(result)
180
1
      return result;
181
146
    sel_org = sel;
182
146
  }
183
184
864
  k = curlx_uztosz(len);
185
186
864
  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
864
    if(strlen(sel) < 1)
190
718
      break;
191
192
146
    result = Curl_xfer_send(data, sel, k, FALSE, &amount);
193
146
    if(!result) { /* Which may not have written it all! */
194
146
      result = Curl_client_write(data, CLIENTWRITE_HEADER, sel, amount);
195
146
      if(result)
196
0
        break;
197
198
146
      k -= amount;
199
146
      sel += amount;
200
146
      if(k < 1)
201
129
        break; /* but it did write it all */
202
146
    }
203
0
    else
204
0
      break;
205
206
17
    timeout_ms = Curl_timeleft(data, NULL, FALSE);
207
17
    if(timeout_ms < 0) {
208
0
      result = CURLE_OPERATION_TIMEDOUT;
209
0
      break;
210
0
    }
211
17
    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
17
    what = SOCKET_WRITABLE(sockfd, timeout_ms);
221
17
    if(what < 0) {
222
0
      result = CURLE_SEND_ERROR;
223
0
      break;
224
0
    }
225
17
    else if(!what) {
226
17
      result = CURLE_OPERATION_TIMEDOUT;
227
17
      break;
228
17
    }
229
17
  }
230
231
864
  free(sel_org);
232
233
864
  if(!result)
234
847
    result = Curl_xfer_send(data, "\r\n", 2, FALSE, &amount);
235
864
  if(result) {
236
17
    failf(data, "Failed sending Gopher request");
237
17
    return result;
238
17
  }
239
847
  result = Curl_client_write(data, CLIENTWRITE_HEADER, "\r\n", 2);
240
847
  if(result)
241
0
    return result;
242
243
847
  Curl_xfer_setup_recv(data, FIRSTSOCKET, -1);
244
847
  return CURLE_OK;
245
847
}
246
#endif /* CURL_DISABLE_GOPHER */