gopher.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 
00023 #include "curl_setup.h"
00024 
00025 #ifndef CURL_DISABLE_GOPHER
00026 
00027 #include "urldata.h"
00028 #include <curl/curl.h>
00029 #include "transfer.h"
00030 #include "sendf.h"
00031 #include "progress.h"
00032 #include "gopher.h"
00033 #include "select.h"
00034 #include "url.h"
00035 #include "escape.h"
00036 #include "warnless.h"
00037 #include "curl_memory.h"
00038 /* The last #include file should be: */
00039 #include "memdebug.h"
00040 
00041 /*
00042  * Forward declarations.
00043  */
00044 
00045 static CURLcode gopher_do(struct connectdata *conn, bool *done);
00046 
00047 /*
00048  * Gopher protocol handler.
00049  * This is also a nice simple template to build off for simple
00050  * connect-command-download protocols.
00051  */
00052 
00053 const struct Curl_handler Curl_handler_gopher = {
00054   "GOPHER",                             /* scheme */
00055   ZERO_NULL,                            /* setup_connection */
00056   gopher_do,                            /* do_it */
00057   ZERO_NULL,                            /* done */
00058   ZERO_NULL,                            /* do_more */
00059   ZERO_NULL,                            /* connect_it */
00060   ZERO_NULL,                            /* connecting */
00061   ZERO_NULL,                            /* doing */
00062   ZERO_NULL,                            /* proto_getsock */
00063   ZERO_NULL,                            /* doing_getsock */
00064   ZERO_NULL,                            /* domore_getsock */
00065   ZERO_NULL,                            /* perform_getsock */
00066   ZERO_NULL,                            /* disconnect */
00067   ZERO_NULL,                            /* readwrite */
00068   PORT_GOPHER,                          /* defport */
00069   CURLPROTO_GOPHER,                     /* protocol */
00070   PROTOPT_NONE                          /* flags */
00071 };
00072 
00073 static CURLcode gopher_do(struct connectdata *conn, bool *done)
00074 {
00075   CURLcode result=CURLE_OK;
00076   struct Curl_easy *data=conn->data;
00077   curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
00078 
00079   curl_off_t *bytecount = &data->req.bytecount;
00080   char *path = data->state.path;
00081   char *sel;
00082   char *sel_org = NULL;
00083   ssize_t amount, k;
00084   size_t len;
00085 
00086   *done = TRUE; /* unconditionally */
00087 
00088   /* Create selector. Degenerate cases: / and /1 => convert to "" */
00089   if(strlen(path) <= 2) {
00090     sel = (char *)"";
00091     len = (int)strlen(sel);
00092   }
00093   else {
00094     char *newp;
00095     size_t j, i;
00096 
00097     /* Otherwise, drop / and the first character (i.e., item type) ... */
00098     newp = path;
00099     newp+=2;
00100 
00101     /* ... then turn ? into TAB for search servers, Veronica, etc. ... */
00102     j = strlen(newp);
00103     for(i=0; i<j; i++)
00104       if(newp[i] == '?')
00105         newp[i] = '\x09';
00106 
00107     /* ... and finally unescape */
00108     result = Curl_urldecode(data, newp, 0, &sel, &len, FALSE);
00109     if(!sel)
00110       return CURLE_OUT_OF_MEMORY;
00111     sel_org = sel;
00112   }
00113 
00114   /* We use Curl_write instead of Curl_sendf to make sure the entire buffer is
00115      sent, which could be sizeable with long selectors. */
00116   k = curlx_uztosz(len);
00117 
00118   for(;;) {
00119     result = Curl_write(conn, sockfd, sel, k, &amount);
00120     if(!result) { /* Which may not have written it all! */
00121       result = Curl_client_write(conn, CLIENTWRITE_HEADER, sel, amount);
00122       if(result)
00123         break;
00124 
00125       k -= amount;
00126       sel += amount;
00127       if(k < 1)
00128         break; /* but it did write it all */
00129     }
00130     else
00131       break;
00132 
00133     /* Don't busyloop. The entire loop thing is a work-around as it causes a
00134        BLOCKING behavior which is a NO-NO. This function should rather be
00135        split up in a do and a doing piece where the pieces that aren't
00136        possible to send now will be sent in the doing function repeatedly
00137        until the entire request is sent.
00138 
00139        Wait a while for the socket to be writable. Note that this doesn't
00140        acknowledge the timeout.
00141     */
00142     if(SOCKET_WRITABLE(sockfd, 100) < 0) {
00143       result = CURLE_SEND_ERROR;
00144       break;
00145     }
00146   }
00147 
00148   free(sel_org);
00149 
00150   if(!result)
00151     /* We can use Curl_sendf to send the terminal \r\n relatively safely and
00152        save allocing another string/doing another _write loop. */
00153     result = Curl_sendf(sockfd, conn, "\r\n");
00154   if(result) {
00155     failf(data, "Failed sending Gopher request");
00156     return result;
00157   }
00158   result = Curl_client_write(conn, CLIENTWRITE_HEADER, (char *)"\r\n", 2);
00159   if(result)
00160     return result;
00161 
00162   Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
00163                       -1, NULL); /* no upload */
00164   return CURLE_OK;
00165 }
00166 #endif /*CURL_DISABLE_GOPHER*/


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:02