X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=src%2Fhttp_command.c;h=dc98b6e0833fb9623f58153ca6af3fe4bb3321dc;hb=06ec84ec5e71464bbdb286b0548483b97147be81;hp=35bed6baa9168dbd4d9867248637e259f0c9f54e;hpb=dbcf2c3410ed74d919efdc62ffbd00e716284d8e;p=pazpar2-moved-to-github.git diff --git a/src/http_command.c b/src/http_command.c index 35bed6b..dc98b6e 100644 --- a/src/http_command.c +++ b/src/http_command.c @@ -1,4 +1,4 @@ -/* $Id: http_command.c,v 1.41 2007-04-23 21:05:23 adam Exp $ +/* $Id: http_command.c,v 1.44 2007-05-23 21:58:28 adam Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -20,7 +20,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA */ /* - * $Id: http_command.c,v 1.41 2007-04-23 21:05:23 adam Exp $ + * $Id: http_command.c,v 1.44 2007-05-23 21:58:28 adam Exp $ */ #include @@ -31,7 +31,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include #include #include - +#include #if HAVE_CONFIG_H #include #endif @@ -103,31 +103,48 @@ void http_session_destroy(struct http_session *s) nmem_destroy(s->nmem); } -static void error(struct http_response *rs, char *code, char *msg, char *txt) +static void error(struct http_response *rs, + const char *code, const char *msg, const char *extra) { struct http_channel *c = rs->channel; - char tmp[1024]; + char text[1024]; + char *sep = extra ? ": " : ""; - if (!txt) - txt = msg; rs->msg = nmem_strdup(c->nmem, msg); strcpy(rs->code, code); - sprintf(tmp, "%s", txt); - rs->payload = nmem_strdup(c->nmem, tmp); + + yaz_snprintf(text, sizeof(text), + "%s%s%s", msg, sep, + extra ? extra : ""); + + yaz_log(YLOG_WARN, "HTTP %s %s%s%s", code, msg, sep, + extra ? extra : ""); + rs->payload = nmem_strdup(c->nmem, text); http_send_response(c); } unsigned int make_sessionid() { - struct timeval t; - unsigned int res; static int seq = 0; + unsigned int res; seq++; - if (gettimeofday(&t, 0) < 0) - abort(); - res = t.tv_sec; - res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1); + if (global_parameters.debug_mode) + res = seq; + else + { + struct timeval t; + + if (gettimeofday(&t, 0) < 0) + { + yaz_log(YLOG_WARN|YLOG_ERRNO, "gettimeofday"); + exit(1); + } + /* at most 256 sessions per second .. + (long long would be more appropriate)*/ + res = t.tv_sec; + res = ((res << 8) | (seq & 0xff)) & ((1U << 31) - 1); + } return res; } @@ -172,8 +189,7 @@ static int process_settings(struct session *se, struct http_request *rq, nmem_strsplit(se->session_nmem, "[]", a->name, &res, &num); if (num != 2) { - error(rs, "417", "Malformed setting argument", 0); - yaz_log(YLOG_WARN, "Malformed setting: %s", a->name); + error(rs, "417", "Malformed setting argument", a->name); return -1; } setting = res[0]; @@ -542,6 +558,31 @@ static void cmd_ping(struct http_channel *c) http_send_response(c); } +static int utf_8_valid(const char *str) +{ + yaz_iconv_t cd = yaz_iconv_open("utf-8", "utf-8"); + if (cd) + { + /* check that query is UTF-8 encoded */ + char *inbuf = (char *) str; /* we know iconv does not alter this */ + size_t inbytesleft = strlen(inbuf); + + size_t outbytesleft = strlen(inbuf) + 10; + char *out = xmalloc(outbytesleft); + char *outbuf = out; + size_t r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); + + /* if OK, try flushing the rest */ + if (r != (size_t) (-1)) + r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft); + yaz_iconv_close(cd); + xfree(out); + if (r == (size_t) (-1)) + return 0; + } + return 1; +} + static void cmd_search(struct http_channel *c) { struct http_request *rq = c->request; @@ -558,10 +599,15 @@ static void cmd_search(struct http_channel *c) error(rs, "417", "Must supply query", 0); return; } + if (!utf_8_valid(query)) + { + error(rs, "417", "Query not UTF-8 encoded", 0); + return; + } res = search(s->psession, query, filter); if (res) { - error(rs, "417", res, res); + error(rs, "417", res, 0); return; } rs->payload = "OK"; @@ -663,7 +709,7 @@ void http_command(struct http_channel *c) break; } if (!commands[i].name) - error(rs, "417", "Unknown command", 0); + error(rs, "417", "Unknown command", command); return; }