]> Nishi Git Mirror - libw3.git/commitdiff
print all
authornishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Fri, 26 Jan 2024 05:52:35 +0000 (05:52 +0000)
committernishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Fri, 26 Jan 2024 05:52:35 +0000 (05:52 +0000)
git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@85 d27a3e52-49c5-7645-884c-6793ebffc270

Example/W3B/w3b.c

index 120bd49f00672f66b0179366b6574b303c66dfa6..ec32d2085d75cf7f04a6a6eac26f116159394cb5 100644 (file)
 #include <string.h>
 #include <unistd.h>
 
+char* databuf;
+int datalen;
+
 void status_handler(struct W3* w3, int status) { printf("Response code: %d\n", status); }
 
-void data_handler(struct W3* w3, char* data, size_t size) {}
+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;
+       }
+}
 
 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_On(w3, "status", (void*)status_handler);
@@ -41,6 +61,8 @@ void access_site(const char* url) {
 
 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) {
@@ -87,6 +109,7 @@ int main(int argc, char** argv) {
                        break;
                case 'p':
                        if(acc){
+                               write(1, databuf, datalen);
                        }
                        break;
                case '\n':