]> code.citadel.org Git - citadel.git/blobdiff - libcitadel/lib/tools.c
Moved parse_url() into libcitadel; I will need it for
[citadel.git] / libcitadel / lib / tools.c
index 3a5e921b6fd8492b7123287a1e0a3665f41a15b9..60168fe91e0ca0cb3cd8c21cc38d775623b43515 100644 (file)
@@ -446,7 +446,7 @@ char *rfc2047encode(char *line, long length)
                return strdup(line);
        }
 
-       result = (char*) malloc(strlen(UTF8_HEADER) + 4 + length * 2);
+       result = (char*) malloc(sizeof(UTF8_HEADER) + 4 + length * 2);
        strncpy (result, UTF8_HEADER, strlen (UTF8_HEADER));
        CtdlEncodeBase64(result + strlen(UTF8_HEADER), line, length, 0);
        end = strlen (result);
@@ -988,7 +988,7 @@ int Ctdl_Utf8StrLen(char *str)
  */
 char *Ctdl_Utf8StrCut(char *str, int maxlen)
 {
-       int n, m = 0;
+       int n = 0, m = 0;
        char *aptr;
 
        if (str == NULL)
@@ -1031,3 +1031,60 @@ void convert_spaces_to_underscores(char *str)
        }
 }
 
+
+/* 
+ * Parse a URL into host, port number, and resource identifier.
+ * (This is used by various functions which might need to fetch web pages.)
+ */
+int parse_url(char *url, char *hostname, int *port, char *identifier)
+{
+       char protocol[1024];
+       char scratch[1024];
+       char *ptr = NULL;
+       char *nptr = NULL;
+       
+       strcpy(scratch, url);
+       ptr = (char *)strchr(scratch, ':');
+       if (!ptr) {
+               return(1);      /* no protocol specified */
+       }
+
+       strcpy(ptr, "");
+       strcpy(protocol, scratch);
+       if (strcmp(protocol, "http")) {
+               return(2);      /* not HTTP */
+       }
+
+       strcpy(scratch, url);
+       ptr = (char *) strstr(scratch, "//");
+       if (!ptr) {
+               return(3);      /* no server specified */
+       }
+       ptr += 2;
+
+       strcpy(hostname, ptr);
+       nptr = (char *)strchr(ptr, ':');
+       if (!nptr) {
+               *port = 80;     /* default */
+               nptr = (char *)strchr(hostname, '/');
+       }
+       else {
+               sscanf(nptr, ":%d", port);
+               nptr = (char *)strchr(hostname, ':');
+       }
+
+       if (nptr) {
+               *nptr = '\0';
+       }
+
+       nptr = (char *)strchr(ptr, '/');
+       
+       if (!nptr) {
+               return(4);      /* no url specified */
+       }
+       
+       strcpy(identifier, nptr);
+       return(0);
+}
+
+