📄 url.mx
字号:
}/* COMMAND "getQueryArg": Extract the argument mappings from the URL query * SIGNATURE: getQueryArg(str) : bat[str,str]; */strurl_getQueryArg(BAT **retval, url t){ char query[1024]; char val[1024]; char name[1024]; char *unescapedval; BAT *b; if (t == 0) throw(ILLARG, "url.getQueryArg", "url missing"); if (unescape_str(&unescapedval, t) != MAL_SUCCEED) throw(MAL, "url.getQueryArg", "failure to unescape"); t = strchr(unescapedval, '?'); if (t == 0) throw(ILLARG, "url.getQueryArg", "variable missing"); t++; b = BATnew(TYPE_str, TYPE_str, 40); if (b == 0) throw(MAL, "url.getQueryArg","could not create BAT"); if (strlen(t) > 1023) throw(PARSE, "url.getQueryArg", "string too long"); strcpy(query, t); for (; query[0] != '\0';) { getword(val, query, '&'); plustospace(val); getword(name, val, '='); BUNins(b, name, val, FALSE); } @:Pseudo(dir,name)@ return MAL_SUCCEED;}/* COMMAND "getRobotURL": Extract the location of the robot control file * SIGNATURE: getRobotURL(str) : str; */strurl_getRobotURL(str *retval, /* put string: pointer to char here. */ url t /* string: pointer to char. */ ){ static char buf[1024]; char *b, *d, *s = buf; int i = 0; if (t == 0) throw(ILLARG, "url.getRobotURL", "url missing"); b = buf; while (*t && *t != ':') *b++ = *t++; *b++ = *t++; if (*t != '/') goto getRobot_done; *b++ = *t++; if (*t != '/') goto getRobot_done; *b++ = *t++; for (; *t && *t != '/'; t++) { *b++ = *t; if (i++ == 1000) throw(PARSE, "url.getRobot", "server name too long"); } strcpy(b, "/robots.txt");getRobot_done: d = (char*) GDKmalloc(strlen(s) + 1); if (d) strcpy(d, s); *retval = d; return MAL_SUCCEED;#if 0 /* not reached */ if (i > 1000) s = (str)str_nil; else strcpy(b, "/robots.txt");#endif}/* COMMAND "getUser": Extract the user identity from the URL * SIGNATURE: getUser(str) : str; */strurl_getUser(str *retval, url t){ static char buf[1024]; char *b, *d = 0, *s; int i = 0; if (t == 0) throw(ILLARG, "url.getUser", "url missing"); s = (str)str_nil; while (*t && *t != ':') t++; t++; if (*t != '/') goto getUser_done; t++; if (*t != '/') goto getUser_done; t++; for (; *t && *t != '/'; t++) ; t++; if (*t == '~') { t++; b = buf; s = buf; for (; *t && *t != '/'; t++) { *b++ = *t; if (i++ == 1023) throw(PARSE, "url.getUser", "server name too long"); } *b = 0; }getUser_done: d = (char*) GDKmalloc(strlen(s) + 1); if (d) strcpy(d, s); *retval = d; return MAL_SUCCEED;}/* COMMAND "isaURL": Check conformity of the URL syntax * SIGNATURE: isaURL(str) : bit; */strurl_isaURL(bit *retval, /* put return atom here. */ url Str1 /* string: pointer to char. */ ){ (void) retval; if (Str1 == 0) throw(ILLARG, "url.isaURL", "url missing"); if (strlen(Str1) == 0) *retval = FALSE; else *retval = TRUE; return MAL_SUCCEED;}int needEscape(char c){ if( isalnum((int)c) ) return 0; if( c == '#' || c == '-' || c == '_' || c == '.' || c == '!' || c == '~' || c == '*' || c == '\'' || c == '(' || c == ')' ) return 0; return 1;}/* COMMAND "escape": this function applies the URI escaping rules defined in * section 2 of [RFC 3986] to the string supplied as 's'. * The effect of the function is to escape a set of identified characters in * the string. Each such character is replaced in the string by an escape * sequence, which is formed by encoding the character as a sequence of octets * in UTF-8, and then reprensenting each of these octets in the form %HH. * * All characters are escaped other than: * [a-z], [A-Z], [0-9], "#", "-", "_", ".", "!", "~", "*", "'", "(", ")" * * This function must always generate hexadecimal values using the upper-case * letters A-F. * * SIGNATURE: escape(str) : str; */strescape_str(str *retval, str s){ int x, y; str res; if (!s) throw(ILLARG, "url.escape", "url missing"); if (!( res = (str) GDKmalloc( strlen(s) * 3 ) )) throw(MAL, "url.escape", "malloc failed"); for (x = 0, y = 0; s[x]; ++x, ++y) { if (needEscape(s[x])){ if (s[x] == ' '){ res[y] = '+'; } else { sprintf(res+y, "%%%2x", s[x]); y += 2; } } else { res[y] = s[x]; } } res[y] = '\0'; *retval = GDKrealloc(res, strlen(res)+1); return MAL_SUCCEED;}/* COMMAND "unescape": Convert hexadecimal representations to ASCII characters. * All sequences of the form "% HEX HEX" are unescaped. * SIGNATURE: unescape(str) : str; */strunescape_str(str *retval, str s){ int x, y; str res; if (!s) throw(ILLARG, "url.escape", "url missing"); res = (str) GDKmalloc(strlen(s)); if (!res) throw(MAL, "url.unescape", "malloc failed"); for (x = 0, y = 0; s[x]; ++x, ++y) { if (s[x] == '%') { res[y] = x2c(&s[x + 1]); x += 2; } else { res[y] = s[x]; } } res[y] = '\0'; *retval = GDKrealloc(res, strlen(res)+1); return MAL_SUCCEED;}/* COMMAND "newurl": Construct a URL from protocol, host,and file * SIGNATURE: newurl(str, str, str) : str; */strurl_new3(str *retval, /* put string: pointer to char here. */ str Protocol, /* string: pointer to char. */ str Server, /* string: pointer to char. */ str File /* string: pointer to char. */ ){ char buf[1024]; str d, s = buf; if (strlen(File) + strlen(Server) + strlen(Protocol) > 1000) s = (str) str_nil; else sprintf(buf, "%s://%s/%s", Protocol, Server, File); d = (str) GDKmalloc(strlen(s) + 1); if (d) strcpy(d, s); *retval = d; return MAL_SUCCEED;}/* COMMAND "newurl": Construct a URL from protocol, host,port,and file * SIGNATURE: newurl(str, str, int, str) : str; */strurl_new4(str *retval, /* put string: pointer to char here. */ str Protocol, /* string: pointer to char. */ str Server, /* string: pointer to char. */ int *Port, /* pointer to integer. */ str File /* string: pointer to char. */ ){ char buf[1024]; str d, s = buf; if (strlen(File) + strlen(Server) + strlen(Protocol) > 1000) s = (str) str_nil; else sprintf(buf, "%s://%s:%d/%s", Protocol, Server, *Port, File); d = (str) GDKmalloc(strlen(s) + 1); if (d) strcpy(d, s); *retval = d; return MAL_SUCCEED;}@+ Utilities@c#define LF 10#define CR 13voidgetword(char *word, char *line, char stop){ int x = 0, y; for (x = 0; ((line[x]) && (line[x] != stop)); x++) word[x] = line[x]; word[x] = '\0'; if (line[x]) ++x; y = 0; while ((line[y++] = line[x++]) != 0) ;}char *makeword(char *line, char stop){ int x = 0, y; char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1)); for (x = 0; ((line[x]) && (line[x] != stop)); x++) word[x] = line[x]; word[x] = '\0'; if (line[x]) ++x; y = 0; while ((line[y++] = line[x++]) != 0) ; return word;}charx2c(char *what){ char digit; digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A') + 10 : (what[0] - '0')); digit *= 16; digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10 : (what[1] - '0')); return (digit);}voidplustospace(char *str){ int x; for (x = 0; str[x]; x++) if (str[x] == '+') str[x] = ' ';}@}@- WrappingHere you find the wrappers around the V4 url library included above.@cintURLfromString(str src, int *len, str *url){ /* actually parse the message for valid url */ if (*url !=0) GDKfree(url); *len = strlen(src); *url = GDKstrdup(src); return *len;}intURLtoString(str *s, int *len, str src){ int l; if (GDK_STRNIL(src)) { **s = 0; return 0; } l = src == 0 ? 3 : strlen(src) + 3; /* if( !*s) *s= (str)GDKmalloc(*len = l); */ if (l >= *len) { GDKfree(*s); *s = (str) GDKmalloc(l); } snprintf(*s, l, "\"%s\"", src); *len = l - 1; return *len;}strURLgetAnchor(str *retval, str *val){ return url_getAnchor(retval, *val);}strURLgetBasename(str *retval, str *t){ return url_getBasename(retval, *t);}strURLgetContent(str *retval, str *Str1){ (void) Str1; /* fool compiler */ *retval = 0; throw(MAL, "url.getContent", "not yet implemented");}strURLgetContext(str *retval, str *val){ return url_getContext(retval, *val);}strURLgetDirectory(int *ret, str *tv){ (void) ret; (void) tv; throw(MAL, "url.getDirectory", "not yet available");/* this operation requres a BAT context, should be defined elsewhere url_getDirectory(ret, *tv);*/}strURLgetExtension(str *retval, str *tv){ return url_getExtension(retval, *tv);}strURLgetFile(str *retval, str *tv){ return url_getFile(retval, *tv);}@+ Url_getHostExtract the server identity from the URLSIGNATURE: getHost(str) : str; @cstrURLgetHost(str *retval, str *tv){ return url_getHost(retval, *tv);}@+ URLgetPort Extract the port id from the URL SIGNATURE: getPort(str) : str; @cstrURLgetPort(str *retval, str *tv){ return url_getPort(retval, *tv);}strURLgetProtocol(str *retval, str *tv){ return url_getProtocol(retval, *tv);}strURLgetQuery(str *retval, str *tv){ return url_getQuery(retval, *tv);}strURLgetQueryArg(int *ret, str *tv){ (void) ret; (void) tv; throw(MAL, "url.getQueryArg", "Not yet implemented");/* url_getQueryArg(ret,*tv); return MAL_SUCCEED;*/}strURLgetRobotURL(str *retval, str *tv){ return url_getRobotURL(retval, *tv);}strURLgetUser(str *retval, str *tv){ return url_getUser(retval, *tv);}strURLisaURL(bit *retval, str *tv){ (void) retval; (void) tv; /* fool compiler */ if (tv == 0) throw(ILLARG, "url.isaURL", "no string"); throw(MAL, "url.isaURL", "not yet implemented");}strURLnew(str *url, str *val){ (void) url; /* fool compiler */ *url = GDKstrdup(*val); return MAL_SUCCEED;}strURLnew3(str *url, str *protocol, str *server, str *file){ int l, i; l = GDK_STRLEN(*file) + GDK_STRLEN(*server) + GDK_STRLEN(*protocol) + 10; *url = (str) GDKmalloc(l); snprintf(*url, l, "%s://", *protocol); i = strlen(*url); snprintf(*url +i, l - i, "%s", *server); i = strlen(*url); snprintf(*url +i, l - i, "/%s", *file); return MAL_SUCCEED;}strURLnew4(str *url, str *protocol, str *server, int *port, str *file){ str Protocol = *protocol; str Server = *server; str File = *file; int l, i; if (GDK_STRNIL(file)) File = ""; if (GDK_STRNIL(server)) Server = ""; if (GDK_STRNIL(protocol)) Protocol = ""; l = strlen(File) + strlen(Server) + strlen(Protocol) + 20; *url = (str) GDKmalloc(l); snprintf(*url, l, "%s://", Protocol); i = strlen(*url); snprintf(*url +i, l - i, "%s", Server); i = strlen(*url); snprintf(*url +i, l - i, ":%d", *port); i = strlen(*url); snprintf(*url +i, l - i, "/%s", File); return MAL_SUCCEED;}@}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -