23 #include <plugin/json_server/http_handler.h>
27 namespace drizzle_plugin
37 _http_response_code=HTTP_OK;
38 _http_response_text=
"OK";
44 bool HttpHandler::handleRequest()
46 evhttp_parse_query(evhttp_request_uri(_req), _req->input_headers);
47 if(_req->type== EVHTTP_REQ_POST )
53 l= evbuffer_remove(_req->input_buffer, buffer, 1024);
54 _query.append(buffer, l);
60 const char* input_query;
61 input_query= (
char *)evhttp_find_header(_req->input_headers,
"query");
62 if(input_query == NULL || strcmp(input_query,
"")==0)
66 _query.append(input_query,strlen(input_query));
69 _schema = (
char *)evhttp_find_header(_req->input_headers,
"schema");
70 _table = (
char *)evhttp_find_header(_req->input_headers,
"table");
71 _id = (
char *)evhttp_find_header(_req->input_headers,
"_id");
76 bool HttpHandler::validate(
string &default_schema,
string &default_table,
bool allow_drop_table)
79 if(not _schema || strcmp(_schema,
"") == 0)
81 _schema = default_schema.c_str();
83 if(not _table || strcmp(_table,
"")==0)
85 _table= default_table.c_str();
87 if(not _table || strcmp(_table,
"")==0)
96 bool retval = reader.
parse(_query,_json_in);
99 _json_out[
"error_type"]=
"json error";
104 if ( !_json_in[
"query"][
"_id"].asBool() )
108 _json_in[
"query"][
"_id"] = (Json::Value::UInt) atol(_id);
112 if((_json_in[
"query"][
"_id"].isNull() || _json_in[
"query"][
"_id"].asString()==
"") && _req->type==EVHTTP_REQ_DELETE && !allow_drop_table)
114 generateDropTableError();
120 void HttpHandler::generateHttpError()
122 _json_out[
"error_type"]=
"http error";
123 _json_out[
"error_message"]=
"table must be specified in URI query string.";
124 _http_response_code = HTTP_NOTFOUND;
125 _http_response_text =
"table must be specified in URI query string.";
128 void HttpHandler::generateDropTableError()
130 _json_out[
"error_type"]=
"http error";
131 _json_out[
"error_message"]=
"_id must be specified in URI query string or set --json_server.allow_drop_table =true";
132 _http_response_code= HTTP_NOTFOUND;
133 _http_response_text=
"_id must be specified in URI query string or set --json_server.allow_drop_table =true";
136 void HttpHandler::sendResponse()
138 struct evbuffer *buf = evbuffer_new();
144 std::string output= writer.
write(_json_out);
145 evbuffer_add(buf, output.c_str(), output.length());
146 evhttp_send_reply( _req, _http_response_code, _http_response_text, buf);