X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=src%2Fhttp_command.c;h=4258b29a2999b3c8a01e370244637cebeefc339d;hb=f506a0514d99a9f8852008bd3bb01baa2bda47cb;hp=e8bba87fba3ed0e41fb0019ecd07486b3676fd6c;hpb=fcf6233e96ba839efd79be52d4c85bab243881be;p=pazpar2-moved-to-github.git diff --git a/src/http_command.c b/src/http_command.c index e8bba87..4258b29 100644 --- a/src/http_command.c +++ b/src/http_command.c @@ -41,6 +41,32 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include "settings.h" #include "client.h" +#ifdef HAVE_MALLINFO +#include + +void print_meminfo(WRBUF wrbuf) +{ + struct mallinfo minfo; + minfo = mallinfo(); + wrbuf_printf(wrbuf, " \n" + " %d\n" + " %d\n" + " %d\n" + " %d\n" + " %d\n" + " %d\n" + " %d\n" + " %d\n" + " %d\n" + " \n", + minfo.arena, minfo.uordblks, minfo.fordblks,minfo.ordblks, minfo.keepcost, minfo.hblks, minfo.hblkhd, minfo.arena + minfo.hblkhd, minfo.uordblks + minfo.hblkhd); + +} +#else +#define print_meminfo(x) +#endif + + // Update this when the protocol changes #define PAZPAR2_PROTOCOL_VERSION "1" @@ -76,7 +102,7 @@ int http_session_use(int delta) g_http_sessions += delta; sessions = g_http_sessions; yaz_mutex_leave(g_http_session_mutex); - yaz_log(YLOG_DEBUG, "%s sesions=%d", delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), sessions); + yaz_log(YLOG_DEBUG, "%s sessions=%d", delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), sessions); return sessions; } @@ -157,7 +183,8 @@ void http_session_destroy(struct http_session *s) yaz_log(http_sessions->log_level, "%p HTTP Session %u destroyed", s, s->session_id); yaz_mutex_enter(http_sessions->mutex); /* only if http_session has no active http sessions on it can be destroyed */ - if (s->destroy_counter == s->activity_counter) { + if (s->destroy_counter == s->activity_counter) + { struct http_session **p = 0; must_destroy = 1; for (p = &http_sessions->session_list; *p; p = &(*p)->next) @@ -246,7 +273,7 @@ unsigned int make_sessionid(void) unsigned int res; seq++; - if (global_parameters.debug_mode) + if (global_parameters.predictable_sessions) res = seq; else { @@ -299,7 +326,9 @@ static struct http_session *locate_session(struct http_channel *c) } // Call after use of locate_session, in order to increment the destroy_counter -static void release_session(struct http_channel *c, struct http_session *session) { +static void release_session(struct http_channel *c, + struct http_session *session) +{ http_sessions_t http_sessions = c->http_sessions; yaz_mutex_enter(http_sessions->mutex); if (session) @@ -339,7 +368,12 @@ static int process_settings(struct session *se, struct http_request *rq, static void cmd_exit(struct http_channel *c) { + char buf[1024]; + struct http_response *rs = c->response; yaz_log(YLOG_WARN, "exit"); + sprintf(buf, HTTP_COMMAND_RESPONSE_PREFIX "OK"); + rs->payload = nmem_strdup(c->nmem, buf); + http_send_response(c); http_close_server(c->server); } @@ -445,13 +479,14 @@ static void cmd_settings(struct http_channel *c) xmlFreeDoc(doc); } - if (process_settings(s->psession, rq, rs) < 0) { - release_session(c,s); + if (process_settings(s->psession, rq, rs) < 0) + { + release_session(c, s); return; } rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "OK"; http_send_response(c); - release_session(c,s); + release_session(c, s); } // Compares two hitsbytarget nodes by hitcount @@ -508,14 +543,11 @@ static void cmd_termlist(struct http_channel *c) struct http_response *rs = c->response; struct http_request *rq = c->request; struct http_session *s = locate_session(c); - struct termlist_score **p; - int len; - int i; const char *name = http_argbyname(rq, "name"); const char *nums = http_argbyname(rq, "num"); int num = 15; int status; - WRBUF debug_log = wrbuf_alloc(); + WRBUF debug_log = 0; if (!s) return; @@ -529,6 +561,8 @@ static void cmd_termlist(struct http_channel *c) if (nums) num = atoi(nums); + debug_log = wrbuf_alloc(); + wrbuf_rewind(c->wrbuf); wrbuf_puts(c->wrbuf, "\n"); @@ -545,17 +579,23 @@ static void cmd_termlist(struct http_channel *c) wrbuf_puts(c->wrbuf, "wrbuf, tname); wrbuf_puts(c->wrbuf, "\">\n"); - if (!strcmp(tname, "xtargets")) { + if (!strcmp(tname, "xtargets")) + { int targets = targets_termlist(c->wrbuf, s->psession, num, c->nmem); wrbuf_printf(debug_log, " xtargets: %d", targets); } else { - p = termlist(s->psession, tname, &len); + int len; + struct termlist_score **p = + get_termlist_score(s->psession, tname, &len); if (p && len) wrbuf_printf(debug_log, " %s: %d", tname, len); - if (p) { - for (i = 0; i < len && i < num; i++){ + if (p) + { + int i; + for (i = 0; i < len && i < num; i++) + { // prevnt sending empty term elements if (!p[i]->term || !p[i]->term[0]) continue; @@ -564,12 +604,12 @@ static void cmd_termlist(struct http_channel *c) wrbuf_puts(c->wrbuf, ""); wrbuf_xmlputs(c->wrbuf, p[i]->term); wrbuf_puts(c->wrbuf, ""); - + wrbuf_printf(c->wrbuf, "%d", p[i]->frequency); wrbuf_puts(c->wrbuf, "\n"); - } + } } } wrbuf_puts(c->wrbuf, "\n"); @@ -582,7 +622,7 @@ static void cmd_termlist(struct http_channel *c) wrbuf_destroy(debug_log); rs->payload = nmem_strdup(rq->channel->nmem, wrbuf_cstr(c->wrbuf)); http_send_response(c); - release_session(c,s); + release_session(c, s); } size_t session_get_memory_status(struct session *session); @@ -596,7 +636,8 @@ static void session_status(struct http_channel *c, struct http_session *s) wrbuf_printf(c->wrbuf, "%zu\n", session_nmem); } -static void cmd_session_status(struct http_channel *c) { +static void cmd_session_status(struct http_channel *c) +{ struct http_response *rs = c->response; struct http_session *s = locate_session(c); if (!s) @@ -608,7 +649,7 @@ static void cmd_session_status(struct http_channel *c) { wrbuf_puts(c->wrbuf, "\n"); rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf)); http_send_response(c); - release_session(c,s); + release_session(c, s); } @@ -632,13 +673,15 @@ static void cmd_server_status(struct http_channel *c) wrbuf_printf(c->wrbuf, " %u\n", clients); /* Only works if yaz has been compiled with enabling of this */ wrbuf_printf(c->wrbuf, " %u\n",resultsets); + print_meminfo(c->wrbuf); /* TODO add all sessions status */ /* http_sessions_t http_sessions = c->http_sessions; */ /* struct http_session *p; */ /* yaz_mutex_enter(http_sessions->mutex); - for (p = http_sessions->session_list; p; p = p->next) { + for (p = http_sessions->session_list; p; p = p->next) + { p->activity_counter++; wrbuf_puts(c->wrbuf, "\n"); wrbuf_printf(c->wrbuf, "%s\n", p->session_id); @@ -697,17 +740,16 @@ static void cmd_bytarget(struct http_channel *c) if (settings && *settings == '1') { wrbuf_puts(c->wrbuf, "\n"); - wrbuf_puts(c->wrbuf, wrbuf_cstr(ht[i].settings_xml)); + wrbuf_puts(c->wrbuf, ht[i].settings_xml); wrbuf_puts(c->wrbuf, "\n"); } wrbuf_puts(c->wrbuf, ""); - wrbuf_destroy(ht[i].settings_xml); } wrbuf_puts(c->wrbuf, ""); rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf)); http_send_response(c); - release_session(c,s); + release_session(c, s); } static void write_metadata(WRBUF w, struct conf_service *service, @@ -1026,7 +1068,8 @@ static void cmd_show(struct http_channel *c) if (block) { - if (!strcmp(block, "preferred") && !session_is_preferred_clients_ready(s->psession) && reclist_get_num_records(s->psession->reclist) == 0) { + if (!strcmp(block, "preferred") && !session_is_preferred_clients_ready(s->psession) && reclist_get_num_records(s->psession->reclist) == 0) + { // if there is already a watch/block. we do not block this one if (session_set_watch(s->psession, SESSION_WATCH_SHOW_PREF, show_records_ready, c, c) != 0) @@ -1034,7 +1077,7 @@ static void cmd_show(struct http_channel *c) yaz_log(c->http_sessions->log_level, "%p Session %u: Blocking on cmd_show. Waiting for preferred targets", s, s->session_id); } - release_session(c,s); + release_session(c, s); return; } @@ -1046,12 +1089,12 @@ static void cmd_show(struct http_channel *c) { yaz_log(c->http_sessions->log_level, "%p Session %u: Blocking on cmd_show", s, s->session_id); } - release_session(c,s); + release_session(c, s); return; } } show_records(c, status); - release_session(c,s); + release_session(c, s); } static void cmd_ping(struct http_channel *c) @@ -1065,31 +1108,6 @@ static void cmd_ping(struct http_channel *c) release_session(c, s); } -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; @@ -1099,6 +1117,7 @@ static void cmd_search(struct http_channel *c) const char *filter = http_argbyname(rq, "filter"); const char *maxrecs = http_argbyname(rq, "maxrecs"); const char *startrecs = http_argbyname(rq, "startrecs"); + const char *limit = http_argbyname(rq, "limit"); enum pazpar2_error_code code; const char *addinfo = 0; @@ -1107,25 +1126,26 @@ static void cmd_search(struct http_channel *c) if (!query) { error(rs, PAZPAR2_MISSING_PARAMETER, "query"); - release_session(c,s); + release_session(c, s); return; } - if (!utf_8_valid(query)) + if (!yaz_utf8_check(query)) { error(rs, PAZPAR2_MALFORMED_PARAMETER_ENCODING, "query"); - release_session(c,s); + release_session(c, s); return; } - code = search(s->psession, query, startrecs, maxrecs, filter, &addinfo); + code = search(s->psession, query, startrecs, maxrecs, filter, limit, + &addinfo); if (code) { error(rs, code, addinfo); - release_session(c,s); + release_session(c, s); return; } rs->payload = HTTP_COMMAND_RESPONSE_PREFIX "OK"; http_send_response(c); - release_session(c,s); + release_session(c, s); } @@ -1144,7 +1164,8 @@ static void cmd_stat(struct http_channel *c) clients = session_active_clients(s->psession); statistics(s->psession, &stat); - if (stat.num_clients > 0) { + if (stat.num_clients > 0) + { progress = (stat.num_clients - clients) / (float)stat.num_clients; } @@ -1164,7 +1185,7 @@ static void cmd_stat(struct http_channel *c) wrbuf_puts(c->wrbuf, ""); rs->payload = nmem_strdup(c->nmem, wrbuf_cstr(c->wrbuf)); http_send_response(c); - release_session(c,s); + release_session(c, s); } static void cmd_info(struct http_channel *c)