Coverage Report

Created: 2023-06-07 07:02

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