123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- #include "curl_setup.h"
- #ifndef CURL_DISABLE_GOPHER
- #include "urldata.h"
- #include <curl/curl.h>
- #include "transfer.h"
- #include "sendf.h"
- #include "progress.h"
- #include "gopher.h"
- #include "select.h"
- #include "url.h"
- #include "escape.h"
- #include "warnless.h"
- #include "curl_memory.h"
- #include "memdebug.h"
- static CURLcode gopher_do(struct connectdata *conn, bool *done);
- const struct Curl_handler Curl_handler_gopher = {
- "GOPHER",
- ZERO_NULL,
- gopher_do,
- ZERO_NULL,
- ZERO_NULL,
- ZERO_NULL,
- ZERO_NULL,
- ZERO_NULL,
- ZERO_NULL,
- ZERO_NULL,
- ZERO_NULL,
- ZERO_NULL,
- ZERO_NULL,
- ZERO_NULL,
- ZERO_NULL,
- PORT_GOPHER,
- CURLPROTO_GOPHER,
- PROTOPT_NONE
- };
- static CURLcode gopher_do(struct connectdata *conn, bool *done)
- {
- CURLcode result = CURLE_OK;
- struct Curl_easy *data = conn->data;
- curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
- curl_off_t *bytecount = &data->req.bytecount;
- char *path = data->state.path;
- char *sel = NULL;
- char *sel_org = NULL;
- ssize_t amount, k;
- size_t len;
- *done = TRUE;
-
- if(strlen(path) <= 2) {
- sel = (char *)"";
- len = strlen(sel);
- }
- else {
- char *newp;
-
- newp = path;
- newp += 2;
-
- result = Curl_urldecode(data, newp, 0, &sel, &len, FALSE);
- if(result)
- return result;
- sel_org = sel;
- }
-
- k = curlx_uztosz(len);
- for(;;) {
- result = Curl_write(conn, sockfd, sel, k, &amount);
- if(!result) {
- result = Curl_client_write(conn, CLIENTWRITE_HEADER, sel, amount);
- if(result)
- break;
- k -= amount;
- sel += amount;
- if(k < 1)
- break;
- }
- else
- break;
-
- if(SOCKET_WRITABLE(sockfd, 100) < 0) {
- result = CURLE_SEND_ERROR;
- break;
- }
- }
- free(sel_org);
- if(!result)
-
- result = Curl_sendf(sockfd, conn, "\r\n");
- if(result) {
- failf(data, "Failed sending Gopher request");
- return result;
- }
- result = Curl_client_write(conn, CLIENTWRITE_HEADER, (char *)"\r\n", 2);
- if(result)
- return result;
- Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
- -1, NULL);
- return CURLE_OK;
- }
- #endif
|