1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2012, 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 http://curl.haxx.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 ***************************************************************************/
22
23#include "curl_setup.h"
24
25#ifndef CURL_DISABLE_GOPHER
26
27#include "urldata.h"
28#include <curl/curl.h>
29#include "transfer.h"
30#include "sendf.h"
31
32#include "progress.h"
33#include "strequal.h"
34#include "gopher.h"
35#include "rawstr.h"
36#include "select.h"
37#include "url.h"
38#include "warnless.h"
39
40#define _MPRINTF_REPLACE /* use our functions only */
41#include <curl/mprintf.h>
42
43#include "curl_memory.h"
44/* The last #include file should be: */
45#include "memdebug.h"
46
47/*
48 * Forward declarations.
49 */
50
51static CURLcode gopher_do(struct connectdata *conn, bool *done);
52
53/*
54 * Gopher protocol handler.
55 * This is also a nice simple template to build off for simple
56 * connect-command-download protocols.
57 */
58
59const struct Curl_handler Curl_handler_gopher = {
60  "GOPHER",                             /* scheme */
61  ZERO_NULL,                            /* setup_connection */
62  gopher_do,                            /* do_it */
63  ZERO_NULL,                            /* done */
64  ZERO_NULL,                            /* do_more */
65  ZERO_NULL,                            /* connect_it */
66  ZERO_NULL,                            /* connecting */
67  ZERO_NULL,                            /* doing */
68  ZERO_NULL,                            /* proto_getsock */
69  ZERO_NULL,                            /* doing_getsock */
70  ZERO_NULL,                            /* domore_getsock */
71  ZERO_NULL,                            /* perform_getsock */
72  ZERO_NULL,                            /* disconnect */
73  ZERO_NULL,                            /* readwrite */
74  PORT_GOPHER,                          /* defport */
75  CURLPROTO_GOPHER,                     /* protocol */
76  PROTOPT_NONE                          /* flags */
77};
78
79static CURLcode gopher_do(struct connectdata *conn, bool *done)
80{
81  CURLcode result=CURLE_OK;
82  struct SessionHandle *data=conn->data;
83  curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
84
85  curl_off_t *bytecount = &data->req.bytecount;
86  char *path = data->state.path;
87  char *sel;
88  char *sel_org = NULL;
89  ssize_t amount, k;
90
91  *done = TRUE; /* unconditionally */
92
93  /* Create selector. Degenerate cases: / and /1 => convert to "" */
94  if(strlen(path) <= 2)
95    sel = (char *)"";
96  else {
97    char *newp;
98    size_t j, i;
99    int len;
100
101    /* Otherwise, drop / and the first character (i.e., item type) ... */
102    newp = path;
103    newp+=2;
104
105    /* ... then turn ? into TAB for search servers, Veronica, etc. ... */
106    j = strlen(newp);
107    for(i=0; i<j; i++)
108      if(newp[i] == '?')
109        newp[i] = '\x09';
110
111    /* ... and finally unescape */
112    sel = curl_easy_unescape(data, newp, 0, &len);
113    if(!sel)
114      return CURLE_OUT_OF_MEMORY;
115    sel_org = sel;
116  }
117
118  /* We use Curl_write instead of Curl_sendf to make sure the entire buffer is
119     sent, which could be sizeable with long selectors. */
120  k = curlx_uztosz(strlen(sel));
121
122  for(;;) {
123    result = Curl_write(conn, sockfd, sel, k, &amount);
124    if(CURLE_OK == result) { /* Which may not have written it all! */
125      result = Curl_client_write(conn, CLIENTWRITE_HEADER, sel, amount);
126      if(result) {
127        Curl_safefree(sel_org);
128        return result;
129      }
130      k -= amount;
131      sel += amount;
132      if(k < 1)
133        break; /* but it did write it all */
134    }
135    else {
136      failf(data, "Failed sending Gopher request");
137      Curl_safefree(sel_org);
138      return result;
139    }
140    /* Don't busyloop. The entire loop thing is a work-around as it causes a
141       BLOCKING behavior which is a NO-NO. This function should rather be
142       split up in a do and a doing piece where the pieces that aren't
143       possible to send now will be sent in the doing function repeatedly
144       until the entire request is sent.
145
146       Wait a while for the socket to be writable. Note that this doesn't
147       acknowledge the timeout.
148    */
149    Curl_socket_ready(CURL_SOCKET_BAD, sockfd, 100);
150  }
151
152  Curl_safefree(sel_org);
153
154  /* We can use Curl_sendf to send the terminal \r\n relatively safely and
155     save allocing another string/doing another _write loop. */
156  result = Curl_sendf(sockfd, conn, "\r\n");
157  if(result != CURLE_OK) {
158    failf(data, "Failed sending Gopher request");
159    return result;
160  }
161  result = Curl_client_write(conn, CLIENTWRITE_HEADER, (char *)"\r\n", 2);
162  if(result)
163    return result;
164
165  Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
166                      -1, NULL); /* no upload */
167  return CURLE_OK;
168}
169#endif /*CURL_DISABLE_GOPHER*/
170