#include "cgit.h"
#include "html.h"
+#include "url.h"
/* Percent-encoding of each character, except: a-zA-Z0-9!$()*,./:;@- */
static const char* url_escape_table[256] = {
void html_txt(const char *txt)
{
- const char *t = txt;
- while (t && *t) {
- int c = *t;
- if (c == '<' || c == '>' || c == '&') {
- html_raw(txt, t - txt);
- if (c == '>')
- html(">");
- else if (c == '<')
- html("<");
- else if (c == '&')
- html("&");
- txt = t + 1;
- }
- t++;
- }
- if (t != txt)
- html(txt);
+ if (txt)
+ html_ntxt(txt, strlen(txt));
}
-void html_ntxt(int len, const char *txt)
+ssize_t html_ntxt(const char *txt, size_t len)
{
const char *t = txt;
- while (t && *t && len--) {
+ ssize_t slen;
+
+ if (len > SSIZE_MAX)
+ return -1;
+
+ slen = (ssize_t) len;
+ while (t && *t && slen--) {
int c = *t;
if (c == '<' || c == '>' || c == '&') {
html_raw(txt, t - txt);
}
if (t != txt)
html_raw(txt, t - txt);
- if (len < 0)
- html("...");
+ return slen;
}
void html_attrf(const char *fmt, ...)
return 0;
}
-static int hextoint(char c)
+void http_parse_querystring(const char *txt, void (*fn)(const char *name, const char *value))
{
- if (c >= 'a' && c <= 'f')
- return 10 + c - 'a';
- else if (c >= 'A' && c <= 'F')
- return 10 + c - 'A';
- else if (c >= '0' && c <= '9')
- return c - '0';
- else
- return -1;
-}
-
-static char *convert_query_hexchar(char *txt)
-{
- int d1, d2, n;
- n = strlen(txt);
- if (n < 3) {
- *txt = '\0';
- return txt-1;
- }
- d1 = hextoint(*(txt + 1));
- d2 = hextoint(*(txt + 2));
- if (d1 < 0 || d2 < 0) {
- memmove(txt, txt + 3, n - 2);
- return txt-1;
- } else {
- *txt = d1 * 16 + d2;
- memmove(txt + 1, txt + 3, n - 2);
- return txt;
- }
-}
+ const char *t = txt;
-int http_parse_querystring(const char *txt_, void (*fn)(const char *name, const char *value))
-{
- char *o, *t, *txt, *value = NULL, c;
-
- if (!txt_)
- return 0;
-
- o = t = txt = xstrdup(txt_);
- while ((c=*t) != '\0') {
- if (c == '=') {
- *t = '\0';
- value = t + 1;
- } else if (c == '+') {
- *t = ' ';
- } else if (c == '%') {
- t = convert_query_hexchar(t);
- } else if (c == '&') {
- *t = '\0';
- (*fn)(txt, value);
- txt = t + 1;
- value = NULL;
+ while (t && *t) {
+ char *name = url_decode_parameter_name(&t);
+ if (*name) {
+ char *value = url_decode_parameter_value(&t);
+ fn(name, value);
+ free(value);
}
- t++;
+ free(name);
}
- if (t != txt)
- (*fn)(txt, value);
- free(o);
- return 0;
}