simplify item type handling, make shorter uris - gopherproxy-c - Gopher HTTP pr… | |
git clone git://git.codemadness.org/gopherproxy-c | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit f3ee251cc87fed2ff5ef4f8b9679c03cad71a5f6 | |
parent 536c656498826de19a9aa266f59831df7ef74b5f | |
Author: Hiltjo Posthuma <[email protected]> | |
Date: Sun, 12 Aug 2018 19:10:23 +0200 | |
simplify item type handling, make shorter uris | |
make shorter uri by removing gopher:// and the port if it is "70". | |
Diffstat: | |
M gopherproxy.c | 70 +++++++++++------------------… | |
1 file changed, 24 insertions(+), 46 deletions(-) | |
--- | |
diff --git a/gopherproxy.c b/gopherproxy.c | |
@@ -19,7 +19,6 @@ | |
#endif | |
struct uri { | |
- char proto[16]; | |
char host[256]; | |
char port[8]; | |
char path[1024]; | |
@@ -294,29 +293,20 @@ servedir(const char *server, const char *port, const char… | |
server, port, path, linenr); | |
} | |
- uri[0] = '\0'; | |
- switch (line[0]) { | |
- case '7': | |
- snprintf(uri, sizeof(uri), "gopher://%s:%s/%c%s", | |
+ if (!strcmp(v.port, "70")) | |
+ snprintf(uri, sizeof(uri), "%s/%c%s", | |
+ v.server, v._type, v.path); | |
+ else | |
+ snprintf(uri, sizeof(uri), "%s:%s/%c%s", | |
v.server, v.port, v._type, v.path); | |
- break; | |
- case 'h': | |
- if (!strncmp(v.path, "URL:", sizeof("URL:") - 1)) | |
- snprintf(uri, sizeof(uri), "%s", v.path + size… | |
- else | |
- snprintf(uri, sizeof(uri), "gopher://%s:%s/%c%… | |
- v.server, v.port, v._type, v.path); | |
- break; | |
+ | |
+ switch (v._type) { | |
case 'i': /* info */ | |
case '3': /* error */ | |
+ fputs(" ", stdout); | |
+ xmlencode(v.username); | |
break; | |
- default: | |
- snprintf(uri, sizeof(uri), "?q=gopher://%s:%s/%c%s", | |
- v.server, v.port, v._type, v.path); | |
- } | |
- | |
- /* search */ | |
- if (v._type == '7') { | |
+ case '7': /* search */ | |
fputs("</pre><form method=\"get\" action=\"\"><pre>", … | |
fputs(typestr(v._type), stdout); | |
fputs(" <input type=\"hidden\" name=\"q\" value=\"", s… | |
@@ -326,18 +316,19 @@ servedir(const char *server, const char *port, const char… | |
fputs( | |
"\" name=\"p\" value=\"\" size=\"72\" />" | |
"<input type=\"submit\" value=\"Search\" /></p… | |
- } else { | |
+ default: /* other */ | |
fputs(typestr(v._type), stdout); | |
- if (uri[0]) { | |
- fputs(" <a href=\"", stdout); | |
- xmlencode(uri); | |
- fputs("\">", stdout); | |
- xmlencode(v.username); | |
- fputs("</a>", stdout); | |
+ fputs(" <a href=\"", stdout); | |
+ if (v._type == 'h' && !strncmp(v.path, "URL:", sizeof(… | |
+ xmlencode(v.path + sizeof("URL:") - 1); | |
} else { | |
- fputs(" ", stdout); | |
- xmlencode(v.username); | |
+ fputs("?q=", stdout); | |
+ xmlencode(uri); | |
} | |
+ fputs("\">", stdout); | |
+ xmlencode(v.username); | |
+ fputs("</a>", stdout); | |
+ | |
} | |
putchar('\n'); | |
} | |
@@ -422,21 +413,8 @@ parseuri(const char *str, struct uri *u) | |
memset(u, 0, sizeof(struct uri)); | |
- /* protocol part */ | |
- for (e = s = str; *e && (isalpha((int)*e) || isdigit((int)*e) || | |
- *e == '+' || *e == '-' || *e == '.'); e++) | |
- ; | |
- if (strncmp(e, "://", sizeof("://") - 1)) | |
- return 0; | |
- if (e - s + 1 >= sizeof(u->proto)) | |
- return 0; | |
- memcpy(u->proto, s, e - s); | |
- u->proto[e - s] = '\0'; | |
- | |
- e += sizeof("://") - 1; | |
- s = e; | |
- | |
- e = &e[strcspn(s, ":/")]; | |
+ s = str; | |
+ e = &s[strcspn(s, ":/")]; | |
if (e - s + 1 >= sizeof(u->host)) | |
return 0; | |
memcpy(u->host, s, e - s); | |
@@ -492,8 +470,8 @@ main(void) | |
path = "/"; | |
if (query[0]) { | |
- if (strncmp(query, "gopher://", sizeof("gopher://") - 1)) | |
- snprintf(uri, sizeof(uri), "gopher://%s", query); | |
+ if (!strncmp(query, "gopher://", sizeof("gopher://") - 1)) | |
+ snprintf(uri, sizeof(uri), "%s", query + sizeof("gophe… | |
else | |
snprintf(uri, sizeof(uri), "%s", query); | |