From 5abb3cae53049a076db299e9d4698acf9b1975cb Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Fri, 27 Apr 2012 21:19:38 -0400 Subject: [PATCH] use common code for gopher read + connect --- gopher.c | 101 ++++++++++++++++--------------------------------------- 1 file changed, 29 insertions(+), 72 deletions(-) diff --git a/gopher.c b/gopher.c index 5e227c3..1b54188 100644 --- a/gopher.c +++ b/gopher.c @@ -5,9 +5,6 @@ #include #include -#include "url.h" -#include "connection.h" -#include "readline2.h" #include #include @@ -16,6 +13,11 @@ #include +#include "url.h" +#include "connection.h" +#include "readline2.h" +#include "prototypes.h" + #include "s16debug.h" @@ -31,34 +33,6 @@ extern int setfiletype(const char *filename); -static int gopher_binary(Word ipid, FILE *file) -{ - // gopher binary support. - // format: raw data until eof. - Word rv = 0; - - for(;;) - { - static char buffer[512]; - rrBuff rb; - Word count; - - TCPIPPoll(); - rv = TCPIPReadTCP(ipid, 0, (Ref)buffer, 512, &rb); - - count = rb.rrBuffCount; - if (rv == 0 && count == 0) continue; - - if (rv && !count) break; - - if (!count) continue; - fwrite(buffer, 1, count, file); - - } - - return rv; -} - static int gopher_text(Word ipid, FILE *file) { // text \r\n @@ -275,48 +249,32 @@ static int gopher_dir(Word ipid, FILE *file) return eof ? 0 : -1; } -void do_gopher(const char *url, URLComponents *components, FILE *file) +int do_gopher(const char *url, URLComponents *components, FILE *file) { - Connection buffer; + Connection connection; char *host; char type; - - LongWord qtick; - + int ok; + + if (!components->portNumber) components->portNumber = 70; - host = malloc(components->host.length + 1); - URLComponentGetC(url, components, URLComponentHost, host); - - ConnectionInit(&buffer, MMStartUp()); - - ConnectionOpenC(&buffer, host, components->portNumber); - - // 30 second timeout. - qtick = GetTick() + 30 * 60; - while (!ConnectionPoll(&buffer)) + host = URLComponentGetCMalloc(url, components, URLComponentHost); + if (!host) { - if (GetTick() >= qtick) - { - fprintf(stderr, "Connection timed out.\n"); - // todo -- still need to close it... - free(host); - return; - } - } - - //s16_debug_tcp(buffer.ipid); + fprintf(stderr, "URL `%s': no host.", url); + return -1; + } - if (buffer.state == kConnectionStateError - || buffer.state == kConnectionStateDisconnected) + ok = ConnectLoop(host, components->portNumber, &connection); + + if (!ok) { - fprintf(stderr, "Unable to open host: %s:%u\n", - host, - components->portNumber); free(host); - return; + return -1; } + // connected.... // path is /[type][resource] @@ -337,39 +295,38 @@ void do_gopher(const char *url, URLComponents *components, FILE *file) { type = url[components->path.location+1]; TCPIPWriteTCP( - buffer.ipid, + connection.ipid, url + components->path.location + 2, components->path.length - 2, 0, 0); } // - TCPIPWriteTCP(buffer.ipid, "\r\n", 2, true, 0); + TCPIPWriteTCP(connection.ipid, "\r\n", 2, true, 0); // 5 and 9 are binary, 1 is dir, all others text. switch(type) { case '1': - gopher_dir(buffer.ipid, file); + gopher_dir(connection.ipid, file); break; case '5': case '9': fsetbinary(file); - gopher_binary(buffer.ipid, file); + ok = read_binary(connection.ipid, file); break; default: - gopher_text(buffer.ipid, file); + ok = gopher_text(connection.ipid, file); break; } fflush(file); - ConnectionClose(&buffer); - - while (!ConnectionPoll(&buffer)) ; // wait for it to close. - - free (host); + CloseLoop(&connection); + free(host); + + return 0; }