/* return pointer to string after the path, filling in pointer to * start of last component and extension of that component; input: * result of skip_authority() */ static const char * skip_path(const char *uri, const char **basep, const char **extp) { const char *base = NULL, *ext = NULL; if (*uri == '/') { uri++; base = uri; while (isunreserved(*uri) || (*uri == '%' && ishex(uri[1]) && ishex(uri[2])) || *uri == ':' || *uri == '@' || *uri == '&' || *uri == '=' || *uri == '+' || *uri == '$' || *uri == ',' || *uri == ';' || *uri == '/') { if (*uri == '/') { base = uri + 1; ext = NULL; } else if (*uri == '.' && ext == NULL && uri != base) { ext = uri; } uri += *uri == '%' ? 3 : 1; } } if (basep) *basep = base; if (extp) *extp = ext; return uri; }
void buffer_add_cchar(buffer *b, int c) { char t[32]; if (isunreserved(c)) buffer_add_char(b, c); else { sprintf(t, "%%%02X", (unsigned char)c); buffer_add_string(b, t); } }
/* return pointer to string after the search string; input: result of * skip_path() */ static const char * skip_search(const char *uri) { if (*uri == '?') { uri++; while (isreserved(*uri) || isunreserved(*uri) || (*uri == '%' && ishex(uri[1]) && ishex(uri[2]))) { uri += *uri == '%' ? 3 : 1; } } return uri; }
void buffer_add_cstring(buffer *b, const char *c) { char t[32]; while (*c) { if (isunreserved(*c)) buffer_add_char(b, *c); else { sprintf(t, "%%%02X", (unsigned char)*c); buffer_add_string(b, t); } c++; } }
char * urlEscape(const char *string, int inlength) { size_t alloc = (inlength?(size_t)inlength:strlen(string))+1; char *ns; char *testing_ptr = NULL; unsigned char in; /* we need to treat the characters unsigned */ size_t newlen = alloc; int strindex=0; size_t length; ns =(char *) malloc(alloc); if(!ns) return NULL; length = alloc-1; while(length--) { in = *string; if (isunreserved(in)) { /* just copy this */ ns[strindex++]=in; } else { /* encode it */ newlen += 2; /* the size grows with two, since this'll become a %XX */ if(newlen > alloc) { alloc *= 2; testing_ptr =(char *) realloc(ns, alloc); if(!testing_ptr) { free( ns ); return NULL; } else { ns = testing_ptr; } } snprintf(&ns[strindex], 4, "%%%02X", in); strindex+=3; } string++; } ns[strindex]=0; /* terminate it */ return ns; }
char *oauth_uri_escape(const char *str) { char *res; int size, ix = 0; if (str == NULL) return mir_strdup(""); size = (int)mir_strlen(str) + 1; res = (char *)mir_alloc(size); while (*str) { if (!isunreserved(*str)) { size += 2; res = (char *)mir_realloc(res, size); mir_snprintf(&res[ix], 4, "%%%X%X", (*str >> 4) & 15, *str & 15); ix += 3; } else
/* return pointer to string after the authority, filling in pointers * to start of user, password, host, and port, if provided; input: * result of skip_scheme() */ static const char * skip_authority(const char *uri, const char **userp, const char **passp, const char **hostp, const char **portp) { const char *user = NULL, *pass = NULL, *host = NULL, *port = NULL; if (uri[0] == '/' && uri[1] == '/') { uri += 2; user = host = uri; while (isunreserved(*uri) || (*uri == '%' && ishex(uri[1]) && ishex(uri[2])) || *uri == ';' || *uri == ':' || *uri == '=' || *uri == '+'|| *uri == '$' || *uri == ',' || *uri == '@') { if (*uri == ':') { if (user == host) port = pass = uri + 1; else port = uri + 1; } else if (*uri == '@') host = uri + 1; uri += *uri == '%' ? 3 : 1; } if (user == host) { /* no "@", so no user info */ if (userp) *userp = NULL; if (passp) *passp = NULL; } else { if (*userp) *userp = user; if (*passp) *passp = pass; } if (portp) *portp = port; if (hostp) *hostp = host; return uri; } return NULL; }