spa/main.c

88 lines
1.8 KiB
C

#include <stdio.h>
#include <string.h>
#include "global.h"
#include "db.h"
#include "url.h"
#include <stdlib.h> //exit
#define ERRLOG 1
FILE* errlog;
extern void get(FILE* f,char*host,U32 port,char* path);
extern void render_file(FILE*in,FILE* out);
/* url_path
Given a mutable full url string, split it, peeling the host.
********/
void fetch_sUrl(FILE* f,sUrl* purl){
char host[256];
U32 host_len = sUrl_host_length(purl);
strncpy(host,purl->buf+purl->oHost,host_len);
host[host_len]=0;
char*px = strchr(host,':'); //port?
if(px) *px=0;
U32 port = sUrl_port(purl);
char* path = sUrl_path(purl);
// printf("fetching: %s %d %s\n",host,port,path);
get(f,host,port,path);
}
sUrl base; // extern'ed in gemtext.c
int doit(char* req){
char buf[1024];
char* p = req;
// If the parameter is exactly 4 long, it is a sigil
if(4==strlen(req)){
U32 idx = sigil_idx(req);
idx_URL(idx,buf);
// printf("idx %4.4X %s\n",idx,buf);
p = buf;
} else { // it must be a full url to start with
if(!url_is_full(req))
return 1;
URL_idx(req); // check the root URL with the db
}
sUrl_set(&base,p,strlen(p));
sUrl_full(&base);
// sUrl_dump(&base);
FILE* fgmi = fopen(TEMPFILE_GMI,"w+");
FILE* fout = stdout;//fopen(TEMPFILE_OUT,"w+");
fetch_sUrl(fgmi,&base);
fseek(fgmi,0,SEEK_SET);
render_file(fgmi,fout);
// fclose(fout);
fclose(fgmi);
// printf("[%s]\n",URL_dir(argv[1],urlbuf));
return 0;
}
extern int index_create();
int main(int argc,char*argv[]){
// printf("ARGC: %d\n",argc);
if(argc != 2){
printf("spa is an experimental Spartan protocol browser\nUsage:\n spa <spartan-url>\n spa <sigil>\n");
return -1;
}
errlog = fopen("errlog.txt","a");
int failed = sys_open();
if(!failed){
failed = doit(argv[1]);
}
sys_close();
fclose(errlog);
return failed;
}