repo: uritools
action: commit
revision: 
path_from: 
revision_from: cd22c61b33351e17e7b06284505365c8afe5e87d:
path_to: 
revision_to: 
git.thebackupbox.net
uritools
git clone git://git.thebackupbox.net/uritools
commit cd22c61b33351e17e7b06284505365c8afe5e87d
Author: epoch 
Date:   Sun Oct 5 01:58:26 2025 -0500

    a flag can be passed to uriescape, ports are checked for validness in string parser

diff --git a/uri.h b/uri.h
index e0dfc31094f5239e39cbb14c38c4097395c01020..
index ..0ce83e652564d4ce03ee944f1f21526ad16511ba 100644
--- a/uri.h
+++ b/uri.h
@@ -9,7 +9,7 @@
 //uri_reserved = gen-delims / sub-delims
 #define pe_gen_delims ":/?#[]@"
 #define pe_sub_delims "!$&'()*+,;="
-//char *pe_reserved[]=pe_gen_delims "" pe_sub_delims; 
+//char *pe_reserved[]=pe_gen_delims "" pe_sub_delims;
 #define pe_ALPHA "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
 #define pe_DIGIT "0123456789"
 #define pe_HPUT "-._~"
@@ -20,7 +20,7 @@ unsigned char rfc3086_percent_encoding[256];
 #define isxdigit(a) ((a >= 'a' && a <= 'f') || (a >= '0' && a <= '9') || (a >= 'A' && a <= 'F'))
 #define toupper(a) ((a >= 'a' && a <= 'z')?a-' ':a)

-char *uri_reserved={
+char *uri_reserved_most={
   pe_gen_delims
   pe_sub_delims
   pe_ALPHA
@@ -28,9 +28,31 @@ char *uri_reserved={
   pe_HPUT
 };

-int uriescapelength(unsigned char *in,int len) {
+char *uri_reserved_least={
+  pe_ALPHA
+  pe_DIGIT
+  pe_HPUT
+};
+
+// returns -1 if not a valid port
+// returns the port number as an integer if it is valid
+signed int stringtoport(char *s) {
+  int port = atoi(s);
+  if(port < 0 || port > 65535) {
+    return -1;
+  }
+  if(port == 0) {
+    if(strcmp(s,"0")) { // we only want to allow the string "0" to be allowed as a zero value.
+      return -1;
+    }
+  }
+  return port;
+}
+
+int uriescapelength(unsigned char *in,int len,char flag) {
   int rlen=0;//be sure to add one to this return value if you plan on putting a null byte at the end.
   int i;
+  char *uri_reserved = flag ? uri_reserved_most : uri_reserved_least;
   for(i=0;iport);
   }
-  if(u->path && u->scheme && !u->domain) {
+  // scheme isn't required
+  if(u->path && !u->domain) {
     strcat(line,u->path);
   }
   if(u->path && u->domain) { // scheme isn't required for path and domain separator to be added
@@ -260,6 +284,12 @@ int urifromline(struct uri *u,char *line) {
     return 1;//we're done here. nothing else to do.
   }

+  // there wasn't a scheme.
+  if((u->query_string || u->fragment_id) && !strchr(line,'/')) {
+    u->path=line;
+    return 1;
+  }
+
   if(*line == '/' && line[1] == '/') {//we have an authority section.
     //let's left-shift this shit over until the third /
     if((u->path=strchr(line+2,'/'))) {
@@ -306,10 +336,14 @@ int urifromline(struct uri *u,char *line) {
       } else {
         u->port=0;//a port isn't actually in the URI!
       }
-    } else { //we're safe to split port off at :
-      if((u->port=strchr(u->domain,':'))) {
-        *u->port=0;
-        u->port++;
+    } else {
+      if((u->port=strrchr(u->domain,':'))) {
+        if(stringtoport(u->port+1) != -1) {
+          *u->port=0;
+          u->port++;
+        } else {
+          u->port=0;// no, that isn't a port number...
+        }
       } //there isn't a port. leave it unset.
     }
     // set but empty should have the domain be empty, but otherwise, domain should be set to null

-----END OF PAGE-----