From: nishi Date: Sun, 28 Jan 2024 23:55:02 +0000 (+0000) Subject: adding X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=3b4abe2254ef175ba43def8aa48fb1c64c32af60;p=libw3.git adding git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@124 d27a3e52-49c5-7645-884c-6793ebffc270 --- diff --git a/Example/Makefile b/Example/Makefile index 4b219b4..49d7a5c 100644 --- a/Example/Makefile +++ b/Example/Makefile @@ -9,14 +9,17 @@ examples: $(MAKE) -C ./fetch CC=$(CC) $(MAKE) -C ./interactive CC=$(CC) $(MAKE) -C ./pop3-list CC=$(CC) + $(MAKE) -C ./w3b CC=$(CC) install: $(MAKE) -C ./fetch CC=$(CC) PREFIX=$(PREFIX) install $(MAKE) -C ./interactive CC=$(CC) PREFIX=$(PREFIX) install $(MAKE) -C ./pop3-list CC=$(CC) PREFIX=$(PREFIX) install + $(MAKE) -C ./w3b CC=$(CC) PREFIX=$(PREFIX) install clean: rm -f *.o *.so *.core *~ *.exe *.res $(MAKE) -C ./fetch clean $(MAKE) -C ./interactive clean $(MAKE) -C ./pop3-list clean + $(MAKE) -C ./w3b clean diff --git a/Example/w3b/Makefile b/Example/w3b/Makefile new file mode 100644 index 0000000..e811bca --- /dev/null +++ b/Example/w3b/Makefile @@ -0,0 +1,18 @@ +# $Id$ +.PHONY: clean install + +./w3b: ./w3b.o $(RESFILE) + $(CC) -g -o $@ -L ../../Library $^ -lw3 + +./%.o: ./%.c + $(CC) -g -c -o $@ -I ../../Library $< + +../libw3.res: + $(MAKE) -C .. ./libw3.res WINDRES=$(WINDRES) + +clean: + rm -f w3b *.o *.so *.core *~ *.exe *.res + +install: ./w3b + mkdir -p $(PREFIX)/bin + cp ./w3b $(PREFIX)/bin/w3b diff --git a/Example/w3b/w3b.c b/Example/w3b/w3b.c new file mode 100644 index 0000000..79b16ac --- /dev/null +++ b/Example/w3b/w3b.c @@ -0,0 +1,136 @@ +/* + * $Id$ + * + * Simple browser + */ + +#include +#include + +#include +#include /* It has some useful functions, you know */ + +#include +#include +#include +#include +#include +#include + +char* databuf; +int datalen; + +void status_handler(struct W3* w3, int status) { printf("Response code: %d\n", status); } +void header_handler(struct W3* w3, char* key, char* value) { printf("Header: %s: %s\n", key, value); } + +void data_handler(struct W3* w3, char* data, size_t size) { + if(databuf == NULL) { + databuf = malloc(size); + datalen = size; + memcpy(databuf, data, size); + } else { + char* oldbuf = databuf; + databuf = malloc(datalen + size); + memcpy(databuf, oldbuf, datalen); + memcpy(databuf + datalen, data, size); + datalen += size; + free(oldbuf); + } +} + +void access_site(const char* url) { + struct W3URL* u = W3_Parse_URL(url); + if(u != NULL) { + struct W3* w3 = W3_Create(u->protocol, u->host, u->port); + if(w3 != NULL) { + if(databuf != NULL) { + free(databuf); + } + databuf = NULL; + datalen = 0; + W3_Set_Method(w3, "GET"); + W3_Set_Path(w3, u->path); + W3_HTTP_Enable_Redirect(w3); + W3_On(w3, "status", (void*)status_handler); + W3_On(w3, "header", (void*)header_handler); + W3_On(w3, "data", (void*)data_handler); + W3_Send_Request(w3); + W3_Free(w3); + printf("%d bytes\n", datalen); + } else { + fprintf(stderr, "Failed to connect\n"); + } + W3_Free_URL(u); + } else { + fprintf(stderr, "Failed to parse\n"); + } +} + +int main(int argc, char** argv) { + int i; + databuf = NULL; + datalen = 0; + char* url = NULL; + for(i = 1; i < argc; i++) { + if(strcmp(argv[i], "--version") == 0) { + printf("LibW3 %s\n", LIBW3_VERSION); + return 0; + } else if(argv[i][0] == '-') { + fprintf(stderr, "%s: unknown option: %s\n", argv[0], argv[i]); + return 1; + } else { + if(url != NULL) { + free(url); + fprintf(stderr, "%s: garbage argument found\n", argv[0]); + return 1; + } + url = __W3_Strdup(argv[i]); + } + } + W3_Library_Init(); + int phase = 0; + char c = 0; + bool acc = false; + if(url != NULL) { + access_site(url); + acc = true; + } + while(true) { /* Loop */ + if(c != '\n' && c != '\r') { + printf("(O)pen, (Q)uit"); + if(acc) printf(", (P)rint all"); + printf("? "); + fflush(stdout); + } + if(scanf("%c", &c) < 0) break; + switch(tolower(c)) { + case 'q': + goto exitnow; + case 'o': + printf("URL: "); + fflush(stdout); + if(url != NULL) free(url); + url = malloc(2049); + scanf("%s", url); + acc = false; + break; + case 'p': + if(acc) { + write(1, databuf, datalen); + } + break; + case '\n': + case '\r': + break; + default: + printf("What do you mean?\n"); + break; + } + if(!acc && url != NULL) { + access_site(url); + acc = true; + } + } + printf("\n"); +exitnow:; +} diff --git a/Makefile b/Makefile index a608658..0e5fa64 100644 --- a/Makefile +++ b/Makefile @@ -112,6 +112,7 @@ install: ./w3.pc archive: all mkdir -p w3-$(VERSION)/Library + mkdir -p w3-$(VERSION)/Example/w3b mkdir -p w3-$(VERSION)/Example/pop3-list mkdir -p w3-$(VERSION)/Example/interactive mkdir -p w3-$(VERSION)/Example/fetch @@ -122,11 +123,13 @@ ifdef WINDOWS cp ./Example/fetch/fetch.exe w3-$(VERSION)/Example/fetch/ cp ./Example/interactive/interactive.exe w3-$(VERSION)/Example/interactive/ cp ./Example/pop3-list/pop3-list.exe w3-$(VERSION)/Example/pop3-list/ + cp ./Example/w3b/w3b.exe w3-$(VERSION)/Example/w3b/ else cp ./Library/*.so w3-$(VERSION)/Library/ cp ./Example/fetch/fetch w3-$(VERSION)/Example/fetch/ cp ./Example/interactive/interactive w3-$(VERSION)/Example/interactive/ cp ./Example/pop3-list/pop3-list w3-$(VERSION)/Example/pop3-list/ + cp ./Example/w3b/w3b w3-$(VERSION)/Example/w3b/ endif -mv w3-$(VERSION)/*.h w3-$(VERSION)/Library/ -mv w3-$(VERSION)/*.so w3-$(VERSION)/Library/