static inline nxweb_result invoke_request_handler(nxweb_http_server_connection* conn, nxweb_http_request* req, nxweb_http_response* resp, nxweb_handler* h, nxweb_handler_flags flags) { if (conn->connection_closing) return; // do not process if already closing if (flags&NXWEB_PARSE_PARAMETERS) nxweb_parse_request_parameters(req, 1); // !!(flags&NXWEB_PRESERVE_URI) if (flags&NXWEB_PARSE_COOKIES) nxweb_parse_request_cookies(req); nxb_start_stream(req->nxb); nxweb_result res=NXWEB_OK; if (h->on_request) { if (flags&NXWEB_INWORKER) { nxw_worker* w=nxw_get_worker(&conn->tdata->workers_factory); if (!w) { nxweb_send_http_error(resp, 503, "Service Unavailable"); nxweb_start_sending_response(conn, resp); return NXWEB_ERROR; } nxe_subscribe(conn->tdata->loop, &w->complete_efs.data_notify, &conn->worker_complete); nxw_start_worker(w, invoke_request_handler_in_worker, conn, &conn->worker_job_done); conn->in_worker=1; } else { res=h->on_request(conn, req, resp); nxd_http_server_proto_finish_response(resp); if (res!=NXWEB_ASYNC) nxweb_start_sending_response(conn, resp); } } return res; }
static nxweb_result download_on_request(nxweb_http_server_connection* conn, nxweb_http_request* req, nxweb_http_response* resp) { nxweb_parse_request_parameters( req, 0 ); const char *name_space = nx_simple_map_get_nocase( req->parameters, "namespace" ); char * url = malloc(strlen(req->path_info)+1); if (!url) {return NXWEB_ERROR;} strcpy(url, req->path_info); url[strlen(req->path_info)] = '\0'; nxweb_url_decode( url, 0 ); char *fname = url, *temp; while (temp = strstr(fname, "/")) {fname = temp + 1;} char *strip_args = strstr(fname, "?"); int fname_len = strip_args ? (strip_args - fname) : strlen(fname); char fname_with_space[1024] = {0}; if ( strlen(url) > sizeof(fname_with_space)-1 ) { char err_msg[1024] = {0}; snprintf( err_msg, sizeof(err_msg)-1, "<html><body>File not found<br/>namespace:%s<br/>filename:%.*s</body></html>", name_space, fname_len, fname ); nxweb_send_http_error(resp, 404, err_msg); resp->keep_alive=0; free(url); return NXWEB_MISS; } if( name_space ) {sprintf( fname_with_space, "%s:/", name_space);} else {strcpy( fname_with_space, ":/" );} strncat( fname_with_space, fname, fname_len ); size_t file_size = 0; char *pfile_data = kcdbget( g_kcdb, fname_with_space, strlen(fname_with_space), &file_size ); if ( pfile_data == NULL ) { char err_msg[1024] = {0}; snprintf( err_msg, sizeof(err_msg)-1, "<html><body>File not found<br/>namespace:%s<br/>filename:%.*s</body></html>", name_space, fname_len, fname ); nxweb_send_http_error(resp, 404, err_msg); resp->keep_alive=0; free(url); return NXWEB_MISS; } KC_DATA *ptmp = nxb_alloc_obj(req->nxb, sizeof(KC_DATA)); nxweb_set_request_data(req, UPLOAD_HANDLER_KEY, (nxe_data)(void *)ptmp, upload_request_data_finalize); ptmp->data_ptr = pfile_data; nxweb_send_data( resp, pfile_data + RECORD_HEADER_LEN, file_size - RECORD_HEADER_LEN, "application/octet-stream" ); free(url); return NXWEB_OK; }
static nxweb_result delete_batch_on_request( nxweb_http_server_connection* conn, nxweb_http_request* req, nxweb_http_response* resp) { nxweb_set_response_content_type(resp, "text/plain"); nxweb_parse_request_parameters( req, 0 ); const char *name_space = nx_simple_map_get_nocase( req->parameters, "namespace" ); const char *fname_raw = nx_simple_map_get_nocase( req->parameters, "names" ); if (!fname_raw) { nxweb_response_printf(resp, "url error\nexample: http://127.0.0.1:8055/deletefiles?names=f1[:f2][&namespace=np]\n"); return NXWEB_ERROR; } KCSTR kc_fnames[400] = {0}; char fnames_with_space[400][1024] = {0}; char *pC = (char *)fname_raw; int fname_cnt = 0; int fname_len = 0; do{ char *pT = strstr( pC, ":" ); if (pT){ fname_len = pT - pC; pT++; } else { fname_len = strlen( pC ); } if (fname_len == 0){pC = pT; continue;} if (name_space) {strcpy(fnames_with_space[fname_cnt], name_space);} strcat(fnames_with_space[fname_cnt], ":/" ); strncat(fnames_with_space[fname_cnt], pC, fname_len); kc_fnames[fname_cnt].buf = (char *)(fnames_with_space + fname_cnt); kc_fnames[fname_cnt].size = strlen(fnames_with_space[fname_cnt]); pC = pT; fname_cnt++; } while(pC); int i=0; for(; i<fname_cnt; ++i) { char pBuf[1024] = {0}; memcpy( pBuf, kc_fnames[i].buf, kc_fnames[i].size ); } kcdbremovebulk( g_kcdb, kc_fnames, fname_cnt, 0 ); nxweb_response_printf( resp, "OK\n" ); }
static nxweb_result delete_on_request( nxweb_http_server_connection* conn, nxweb_http_request* req, nxweb_http_response* resp) { nxweb_set_response_content_type(resp, "text/plain"); nxweb_parse_request_parameters(req, 0); const char *name_space = nx_simple_map_get_nocase(req->parameters, "namespace"); char * url = malloc(strlen(req->path_info)+1); if (!url) { nxweb_send_http_error(resp, 500, "Please retry"); resp->keep_alive=0; return NXWEB_ERROR; } strcpy(url, req->path_info); url[strlen(req->path_info)] = '\0'; nxweb_url_decode(url, 0); char *fname = url, *temp; while (temp = strstr(fname, "/")) {fname = temp + 1;} char *strip_args = strstr(fname, "?"); int fname_len = strip_args?strip_args-fname:strlen(fname); char fname_with_space[1024]= {0}; if (strlen(url) > sizeof(fname_with_space)-1) { nxweb_send_http_error(resp, 414, "Request URI Too Long"); resp->keep_alive=0; free(url); return NXWEB_MISS; } if (name_space) {sprintf( fname_with_space, "%s:/", name_space);} else {strcpy(fname_with_space, ":/");} strncat(fname_with_space, fname, fname_len); kcdbremove(g_kcdb, fname_with_space, strlen( fname_with_space)); nxweb_response_printf(resp, "OK\n"); free(url); return NXWEB_OK; }