]> Nishi Git Mirror - libw3.git/commitdiff
fixing ftp stuff
authornishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Wed, 14 Feb 2024 01:34:43 +0000 (01:34 +0000)
committernishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Wed, 14 Feb 2024 01:34:43 +0000 (01:34 +0000)
git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@214 d27a3e52-49c5-7645-884c-6793ebffc270

Example/ftp-list/ftp-list.c
Library/Core.c
Library/FTP.c
Library/URL.c
W3Version.h.p

index cf8937f4608cc20eb0a3939afc3b1ba450ee9132..10201b7fdd5a565af3295a4c28c2465d9e571a4f 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * $Id$
  *
- * Lists the FTP files 
+ * Lists the FTP files
  */
 
 #include <W3Core.h>
 #include <string.h>
 #include <unistd.h>
 
-void resp_handler(struct W3* w3, int status, char* data){
+struct W3URL* w3url;
+
+void resp_handler(struct W3* w3, int status, char* data) {
        printf("%d\n%s\n", status, data);
-       if(status == 230){
+       if(status == 230) {
+               W3_Set_Method(w3, "CWD");
+               W3_Set_Path(w3, w3url->path);
+               W3_FTP_Send_Request(w3);
                W3_Set_Method(w3, "LIST");
                W3_Set_Path(w3, "/");
                W3_FTP_Send_Request(w3);
-       }else if(status == 226){
+       } else if(status == 226) {
                W3_FTP_Disconnect(w3);
        }
 }
 
-void data_handler(struct W3* w3, char* data, size_t size){
-       write(1, data, size);
-}
+void data_handler(struct W3* w3, char* data, size_t size) { write(1, data, size); }
 
 int main(int argc, char** argv) {
        if(argc < 2) {
@@ -38,18 +41,18 @@ int main(int argc, char** argv) {
                return 1;
        }
        W3_Library_Init();
-       struct W3URL* w3url = W3_Parse_URL(argv[1]);
-       if(w3url != NULL){
+       w3url = W3_Parse_URL(argv[1]);
+       if(w3url != NULL) {
                bool err = false;
-               if(w3url->username == NULL){
+               if(w3url->username == NULL) {
                        err = true;
                        fprintf(stderr, "%s: missing username\n", argv[0]);
                }
-               if(w3url->password == NULL){
+               if(w3url->password == NULL) {
                        err = true;
                        fprintf(stderr, "%s: missing password\n", argv[0]);
                }
-               if(err){
+               if(err) {
                        W3_Free_URL(w3url);
                        return 1;
                }
@@ -60,7 +63,7 @@ int main(int argc, char** argv) {
                W3_On(w3, "data", data_handler);
                W3_Send_Request(w3);
                W3_Free_URL(w3url);
-       }else{
+       } else {
                return 1;
        }
 }
index 8f0d6d9693467709511b23dde682e8a517b03f64..6e78d3ba424339d43a4497bb0cb08323cc8043c0 100644 (file)
@@ -5,13 +5,13 @@
 #include "W3Util.h"
 #include "W3Version.h"
 
+#include "W3FTP.h"
 #include "W3File.h"
 #include "W3Finger.h"
 #include "W3Gopher.h"
 #include "W3HTTP.h"
-#include "W3POP3.h"
 #include "W3Nex.h"
-#include "W3FTP.h"
+#include "W3POP3.h"
 #ifdef SSL_SUPPORT
 #include "W3Gemini.h"
 #endif
index 4f275dcc653d2f23b7a9e7948c6c4d41fb70d794..9cd4b2177467a60718833fe9270623efe3714c17 100644 (file)
@@ -8,11 +8,19 @@
 #include <stdlib.h>
 #include <string.h>
 
+#ifdef __MINGW32__
+#include <windows.h>
+#include <winsock2.h>
+#else
+#include <netdb.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <sys/utsname.h>
+#endif
+
 extern int strcasecmp(const char* s1, const char* s2);
 
-void __W3_FTP_Start_Passive(struct W3* w3){
-       __W3_Auto_Write(w3, "PASV\r\n", 6);
-}
+void __W3_FTP_Start_Passive(struct W3* w3) { __W3_Auto_Write(w3, "PASV\r\n", 6); }
 
 void __W3_FTP_Request(struct W3* w3) {
        if(__W3_Get_Prop(w3, "FTP_USERNAME") == NULL || __W3_Get_Prop(w3, "FTP_PASSWORD") == NULL) {
@@ -41,38 +49,38 @@ void __W3_FTP_Request(struct W3* w3) {
                int len = __W3_Auto_Read(w3, buf, w3->readsize);
                if(len <= 0) break;
                int i;
-               for(i = 0; i < len; i++){
-                       if((buf[i] == ' ' || buf[i] == '-') && !gotstatus){
+               for(i = 0; i < len; i++) {
+                       if((buf[i] == ' ' || buf[i] == '-') && !gotstatus) {
                                cont = buf[i] == '-';
                                gotstatus = true;
-                       }else if(!gotstatus){
+                       } else if(!gotstatus) {
                                status = status * 10;
                                status += buf[i] - '0';
-                       }else if(buf[i] != '\r' && buf[i] != '\n'){
+                       } else if(buf[i] != '\r' && buf[i] != '\n') {
                                cbuf[0] = buf[i];
                                char* tmp = buffer;
                                buffer = __W3_Concat(tmp, cbuf);
                                free(tmp);
-                       }else if(buf[i] == '\n'){
+                       } else if(buf[i] == '\n') {
                                gotstatus = false;
                                cbuf[0] = '\n';
                                char* tmp = buffer;
                                buffer = __W3_Concat(tmp, cbuf);
                                free(tmp);
-                               if(!cont){
-                                       if(status == 220 && auth == 0){
+                               if(!cont) {
+                                       if(status == 220 && auth == 0) {
                                                __W3_Auto_Write(w3, "USER ", 5);
                                                __W3_Auto_Write(w3, __W3_Get_Prop(w3, "FTP_USERNAME"), strlen(__W3_Get_Prop(w3, "FTP_USERNAME")));
                                                __W3_Auto_Write(w3, "\r\n", 2);
                                                auth = 1;
-                                       }else if(status == 331 && auth == 1){
+                                       } else if(status == 331 && auth == 1) {
                                                __W3_Auto_Write(w3, "PASS ", 5);
                                                __W3_Auto_Write(w3, __W3_Get_Prop(w3, "FTP_PASSWORD"), strlen(__W3_Get_Prop(w3, "FTP_PASSWORD")));
                                                __W3_Auto_Write(w3, "\r\n", 2);
                                                auth = 2;
-                                       }else if(status == 230 && auth == 2){
-                                               auth = 3;       
-                                       }else if(auth < 3){
+                                       } else if(status == 230 && auth == 2) {
+                                               auth = 3;
+                                       } else if(auth < 3) {
                                                __W3_Debug("LibW3-FTP", "Login failed");
                                                void* funcptr = __W3_Get_Event(w3, "error");
                                                if(funcptr != NULL) {
@@ -80,8 +88,8 @@ void __W3_FTP_Request(struct W3* w3) {
                                                        func(w3, "login-fail");
                                                }
                                                goto ftp_stop;
-                                       }else if(auth == 3){
-                                               if(status == 227 && (cond & 1)){
+                                       } else if(auth == 3) {
+                                               if(status == 227 && (cond & 1)) {
                                                        /* Passive mode */
                                                        int j;
                                                        int cnt = 0;
@@ -91,18 +99,18 @@ void __W3_FTP_Request(struct W3* w3) {
                                                        char* numbuf = malloc(1);
                                                        numbuf[0] = 0;
                                                        int port = 0;
-                                                       for(j = 0; buffer[j] != 0; j++){
-                                                               if(buffer[j] == '('){
+                                                       for(j = 0; buffer[j] != 0; j++) {
+                                                               if(buffer[j] == '(') {
                                                                        pasv = true;
-                                                               }else if(pasv){
-                                                                       if(buffer[j] == ',' || (pasv && buffer[j] == ')')){
+                                                               } else if(pasv) {
+                                                                       if(buffer[j] == ',' || (pasv && buffer[j] == ')')) {
                                                                                cnt++;
-                                                                               if(cnt < 4){    
+                                                                               if(cnt < 4) {
                                                                                        cbuf[0] = '.';
                                                                                        char* tmp = addr;
                                                                                        addr = __W3_Concat(tmp, cbuf);
                                                                                        free(tmp);
-                                                                               }else{
+                                                                               } else {
                                                                                        port = port << 8;
                                                                                        port |= atoi(numbuf);
                                                                                        free(numbuf);
@@ -110,13 +118,13 @@ void __W3_FTP_Request(struct W3* w3) {
                                                                                        numbuf[0] = 0;
                                                                                }
                                                                                if(buffer[j] == ')') break;
-                                                                       }else{
-                                                                               if(cnt < 4){
+                                                                       } else {
+                                                                               if(cnt < 4) {
                                                                                        cbuf[0] = buffer[j];
                                                                                        char* tmp = addr;
                                                                                        addr = __W3_Concat(tmp, cbuf);
                                                                                        free(tmp);
-                                                                               }else{
+                                                                               } else {
                                                                                        cbuf[0] = buffer[j];
                                                                                        char* tmp = numbuf;
                                                                                        numbuf = __W3_Concat(tmp, cbuf);
@@ -128,17 +136,16 @@ void __W3_FTP_Request(struct W3* w3) {
                                                        free(numbuf);
                                                        pasvsock = __W3_DNS_Connect(addr, false, port,
 #ifdef SSL_SUPPORT
-                                                               NULL, NULL
+                                                                                   NULL, NULL
 #endif
                                                        );
                                                        free(addr);
-                                                       if(strcasecmp(w3->method, "LIST") == 0){
-                                                               __W3_Auto_Write(w3, "LIST ", 5);
-                                                               __W3_Auto_Write(w3, w3->path, strlen(w3->path));
+                                                       if(strcasecmp(w3->method, "LIST") == 0) {
+                                                               __W3_Auto_Write(w3, "LIST", 4);
                                                                __W3_Auto_Write(w3, "\r\n", 2);
                                                        }
                                                        char* ftpbuf = malloc(w3->readsize);
-                                                       while(true){
+                                                       while(true) {
                                                                int ftplen = recv(pasvsock, ftpbuf, w3->readsize, 0);
                                                                if(ftplen <= 0) break;
                                                                void* funcptr = __W3_Get_Event(w3, "data");
@@ -148,6 +155,9 @@ void __W3_FTP_Request(struct W3* w3) {
                                                                }
                                                        }
                                                        free(ftpbuf);
+                                                       cond &= ~1;
+                                                       free(w3->method);
+                                                       w3->method = NULL;
                                                }
                                        }
                                        void* funcptr = __W3_Get_Event(w3, "ftpresp");
@@ -174,9 +184,13 @@ void W3_FTP_Set_Username(struct W3* w3, const char* username) { __W3_Add_Prop(w3
 void W3_FTP_Set_Password(struct W3* w3, const char* password) { __W3_Add_Prop(w3, "FTP_PASSWORD", password); }
 
 void W3_FTP_Send_Request(struct W3* w3) {
-       if(strcasecmp(w3->method, "LIST") == 0){
+       if(strcasecmp(w3->method, "LIST") == 0) {
                __W3_FTP_Start_Passive(w3);
                *((int*)w3->generic) |= 1;
+       } else if(strcasecmp(w3->method, "CWD") == 0) {
+               __W3_Auto_Write(w3, "CWD ", 4);
+               __W3_Auto_Write(w3, w3->path, strlen(w3->path));
+               __W3_Auto_Write(w3, "\r\n", 2);
        }
 }
 
index 26267203f3738e031393b3e89ebffea5b0436ed1..cf214b6ba89bc0449e7e67b2df6906157060e25e 100644 (file)
@@ -84,13 +84,13 @@ struct W3URL* W3_Parse_URL(const char* _url) {
                                        }
                                }
                                free(cbuf);
-                               if(r->username != NULL){
+                               if(r->username != NULL) {
                                        char* str = malloc(64 + strlen(r->username));
                                        sprintf(str, "Username is %s", r->username);
                                        __W3_Debug("URL", str);
                                        free(str);
                                }
-                               if(r->password != NULL){
+                               if(r->password != NULL) {
                                        char* str = malloc(64 + strlen(r->password));
                                        sprintf(str, "Password is %s", r->password);
                                        __W3_Debug("URL", str);
@@ -124,13 +124,11 @@ struct W3URL* W3_Parse_URL(const char* _url) {
                                        r->port = 1965;
                                } else if(strcmp(r->protocol, "finger") == 0) {
                                        r->port = 79;
-                               }else if(strcmp(r->protocol, "nex") == 0) {
+                               } else if(strcmp(r->protocol, "nex") == 0) {
                                        r->port = 1900;
-                               }else if(strcmp(r->protocol, "ftp") == 0) {
+                               } else if(strcmp(r->protocol, "ftp") == 0) {
                                        r->port = 21;
                                }
-
-
                        }
                        r->host = __W3_Strdup(url + start + (atmark == 0 ? 0 : (atmark - 1)));
                        char* str = malloc(strlen(r->host) + 64);
index c2b02bc840fa03a2881907c18c71679ac962a743..81d7988518ea28a889a59f0e2e85b7c3609be6b2 100644 (file)
@@ -6,7 +6,7 @@
 extern "C" {
 #endif
 
-#define LIBW3_VERSION "2.11A" \
+#define LIBW3_VERSION "2.11B" \
 SUFFIX
 
 #ifdef __cplusplus