Coverage Report

Created: 2025-11-11 06:28

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 "vtls/vtls.h"
39
#include "url.h"
40
#include "escape.h"
41
#include "curlx/warnless.h"
42
43
/* The last 2 #include files should be: */
44
#include "curl_memory.h"
45
#include "memdebug.h"
46
47
/*
48
 * Forward declarations.
49
 */
50
51
static CURLcode gopher_do(struct Curl_easy *data, bool *done);
52
#ifdef USE_SSL
53
static CURLcode gopher_connect(struct Curl_easy *data, bool *done);
54
static CURLcode gopher_connecting(struct Curl_easy *data, bool *done);
55
#endif
56
57
/*
58
 * Gopher protocol handler.
59
 * This is also a nice simple template to build off for simple
60
 * connect-command-download protocols.
61
 */
62
63
const struct Curl_handler Curl_handler_gopher = {
64
  "gopher",                             /* scheme */
65
  ZERO_NULL,                            /* setup_connection */
66
  gopher_do,                            /* do_it */
67
  ZERO_NULL,                            /* done */
68
  ZERO_NULL,                            /* do_more */
69
  ZERO_NULL,                            /* connect_it */
70
  ZERO_NULL,                            /* connecting */
71
  ZERO_NULL,                            /* doing */
72
  ZERO_NULL,                            /* proto_pollset */
73
  ZERO_NULL,                            /* doing_pollset */
74
  ZERO_NULL,                            /* domore_pollset */
75
  ZERO_NULL,                            /* perform_pollset */
76
  ZERO_NULL,                            /* disconnect */
77
  ZERO_NULL,                            /* write_resp */
78
  ZERO_NULL,                            /* write_resp_hd */
79
  ZERO_NULL,                            /* connection_check */
80
  ZERO_NULL,                            /* attach connection */
81
  ZERO_NULL,                            /* follow */
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_pollset */
99
  ZERO_NULL,                            /* doing_pollset */
100
  ZERO_NULL,                            /* domore_pollset */
101
  ZERO_NULL,                            /* perform_pollset */
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
  ZERO_NULL,                            /* follow */
108
  PORT_GOPHER,                          /* defport */
109
  CURLPROTO_GOPHERS,                    /* protocol */
110
  CURLPROTO_GOPHER,                     /* family */
111
  PROTOPT_SSL                           /* flags */
112
};
113
114
static CURLcode gopher_connect(struct Curl_easy *data, bool *done)
115
0
{
116
0
  (void)data;
117
0
  (void)done;
118
0
  return CURLE_OK;
119
0
}
120
121
static CURLcode gopher_connecting(struct Curl_easy *data, bool *done)
122
0
{
123
0
  struct connectdata *conn = data->conn;
124
0
  CURLcode result;
125
126
0
  result = Curl_conn_connect(data, FIRSTSOCKET, TRUE, done);
127
0
  if(result)
128
0
    connclose(conn, "Failed TLS connection");
129
0
  *done = TRUE;
130
0
  return result;
131
0
}
132
#endif
133
134
static CURLcode gopher_do(struct Curl_easy *data, bool *done)
135
925
{
136
925
  CURLcode result = CURLE_OK;
137
925
  struct connectdata *conn = data->conn;
138
925
  curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
139
925
  char *gopherpath;
140
925
  char *path = data->state.up.path;
141
925
  char *query = data->state.up.query;
142
925
  char *sel = NULL;
143
925
  char *sel_org = NULL;
144
925
  timediff_t timeout_ms;
145
925
  ssize_t k;
146
925
  size_t amount, len;
147
925
  int what;
148
149
925
  *done = TRUE; /* unconditionally */
150
151
  /* path is guaranteed non-NULL */
152
925
  DEBUGASSERT(path);
153
154
925
  if(query)
155
88
    gopherpath = curl_maprintf("%s?%s", path, query);
156
837
  else
157
837
    gopherpath = strdup(path);
158
159
925
  if(!gopherpath)
160
0
    return CURLE_OUT_OF_MEMORY;
161
162
  /* Create selector. Degenerate cases: / and /1 => convert to "" */
163
925
  if(strlen(gopherpath) <= 2) {
164
761
    sel = (char *)CURL_UNCONST("");
165
761
    len = strlen(sel);
166
761
    free(gopherpath);
167
761
  }
168
164
  else {
169
164
    char *newp;
170
171
    /* Otherwise, drop / and the first character (i.e., item type) ... */
172
164
    newp = gopherpath;
173
164
    newp += 2;
174
175
    /* ... and finally unescape */
176
164
    result = Curl_urldecode(newp, 0, &sel, &len, REJECT_ZERO);
177
164
    free(gopherpath);
178
164
    if(result)
179
1
      return result;
180
163
    sel_org = sel;
181
163
  }
182
183
924
  k = curlx_uztosz(len);
184
185
924
  for(;;) {
186
    /* Break out of the loop if the selector is empty because OpenSSL and/or
187
       LibreSSL fail with errno 0 if this is the case. */
188
924
    if(strlen(sel) < 1)
189
761
      break;
190
191
163
    result = Curl_xfer_send(data, sel, k, FALSE, &amount);
192
163
    if(!result) { /* Which may not have written it all! */
193
163
      result = Curl_client_write(data, CLIENTWRITE_HEADER, sel, amount);
194
163
      if(result)
195
0
        break;
196
197
163
      k -= amount;
198
163
      sel += amount;
199
163
      if(k < 1)
200
148
        break; /* but it did write it all */
201
163
    }
202
0
    else
203
0
      break;
204
205
15
    timeout_ms = Curl_timeleft(data, NULL, FALSE);
206
15
    if(timeout_ms < 0) {
207
0
      result = CURLE_OPERATION_TIMEDOUT;
208
0
      break;
209
0
    }
210
15
    if(!timeout_ms)
211
0
      timeout_ms = TIMEDIFF_T_MAX;
212
213
    /* Do not busyloop. The entire loop thing is a work-around as it causes a
214
       BLOCKING behavior which is a NO-NO. This function should rather be
215
       split up in a do and a doing piece where the pieces that are not
216
       possible to send now will be sent in the doing function repeatedly
217
       until the entire request is sent.
218
    */
219
15
    what = SOCKET_WRITABLE(sockfd, timeout_ms);
220
15
    if(what < 0) {
221
0
      result = CURLE_SEND_ERROR;
222
0
      break;
223
0
    }
224
15
    else if(!what) {
225
15
      result = CURLE_OPERATION_TIMEDOUT;
226
15
      break;
227
15
    }
228
15
  }
229
230
924
  free(sel_org);
231
232
924
  if(!result)
233
909
    result = Curl_xfer_send(data, "\r\n", 2, FALSE, &amount);
234
924
  if(result) {
235
15
    failf(data, "Failed sending Gopher request");
236
15
    return result;
237
15
  }
238
909
  result = Curl_client_write(data, CLIENTWRITE_HEADER, "\r\n", 2);
239
909
  if(result)
240
0
    return result;
241
242
909
  Curl_xfer_setup_recv(data, FIRSTSOCKET, -1);
243
909
  return CURLE_OK;
244
909
}
245
#endif /* CURL_DISABLE_GOPHER */