use common code for gopher read + connect

This commit is contained in:
Kelvin Sherlock 2012-04-27 21:19:38 -04:00
parent aabed2e462
commit 5abb3cae53
1 changed files with 29 additions and 72 deletions

101
gopher.c
View File

@ -5,9 +5,6 @@
#include <MiscTool.h> #include <MiscTool.h>
#include <tcpip.h> #include <tcpip.h>
#include "url.h"
#include "connection.h"
#include "readline2.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -16,6 +13,11 @@
#include <unistd.h> #include <unistd.h>
#include "url.h"
#include "connection.h"
#include "readline2.h"
#include "prototypes.h"
#include "s16debug.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) static int gopher_text(Word ipid, FILE *file)
{ {
// text \r\n // text \r\n
@ -275,48 +249,32 @@ static int gopher_dir(Word ipid, FILE *file)
return eof ? 0 : -1; 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 *host;
char type; char type;
int ok;
LongWord qtick;
if (!components->portNumber) components->portNumber = 70; if (!components->portNumber) components->portNumber = 70;
host = malloc(components->host.length + 1); host = URLComponentGetCMalloc(url, components, URLComponentHost);
URLComponentGetC(url, components, URLComponentHost, host); if (!host)
ConnectionInit(&buffer, MMStartUp());
ConnectionOpenC(&buffer, host, components->portNumber);
// 30 second timeout.
qtick = GetTick() + 30 * 60;
while (!ConnectionPoll(&buffer))
{ {
if (GetTick() >= qtick) fprintf(stderr, "URL `%s': no host.", url);
{ return -1;
fprintf(stderr, "Connection timed out.\n"); }
// todo -- still need to close it...
free(host);
return;
}
}
//s16_debug_tcp(buffer.ipid);
if (buffer.state == kConnectionStateError ok = ConnectLoop(host, components->portNumber, &connection);
|| buffer.state == kConnectionStateDisconnected)
if (!ok)
{ {
fprintf(stderr, "Unable to open host: %s:%u\n",
host,
components->portNumber);
free(host); free(host);
return; return -1;
} }
// connected.... // connected....
// path is /[type][resource] // path is /[type][resource]
@ -337,39 +295,38 @@ void do_gopher(const char *url, URLComponents *components, FILE *file)
{ {
type = url[components->path.location+1]; type = url[components->path.location+1];
TCPIPWriteTCP( TCPIPWriteTCP(
buffer.ipid, connection.ipid,
url + components->path.location + 2, url + components->path.location + 2,
components->path.length - 2, components->path.length - 2,
0, 0,
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. // 5 and 9 are binary, 1 is dir, all others text.
switch(type) switch(type)
{ {
case '1': case '1':
gopher_dir(buffer.ipid, file); gopher_dir(connection.ipid, file);
break; break;
case '5': case '5':
case '9': case '9':
fsetbinary(file); fsetbinary(file);
gopher_binary(buffer.ipid, file); ok = read_binary(connection.ipid, file);
break; break;
default: default:
gopher_text(buffer.ipid, file); ok = gopher_text(connection.ipid, file);
break; break;
} }
fflush(file); fflush(file);
ConnectionClose(&buffer); CloseLoop(&connection);
free(host);
while (!ConnectionPoll(&buffer)) ; // wait for it to close.
return 0;
free (host);
} }