drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
http_handler.cc
1/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2012 Mohit Srivastava
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
23#include <plugin/json_server/http_handler.h>
24
25using namespace std;
26
27namespace drizzle_plugin
28{
29namespace json_server
30{
31 HttpHandler::HttpHandler(Json::Value& json_out,Json::Value json_in,struct evhttp_request *req)
32 {
33 _schema=NULL;
34 _table=NULL;
35 _id=NULL;
36 _query="";
37 _http_response_code=HTTP_OK;
39 _json_out=json_out;
40 _json_in=json_in;
41 _req=req;
42 }
43
45 {
46 evhttp_parse_query(evhttp_request_uri(_req), _req->input_headers);
47 if(_req->type== EVHTTP_REQ_POST )
48 {
49 char buffer[1024];
50 int l=0;
51 do
52 {
53 l= evbuffer_remove(_req->input_buffer, buffer, 1024);
54 _query.append(buffer, l);
55 }
56 while(l);
57 }
58 else
59 {
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)
63 {
64 input_query="{}";
65 }
66 _query.append(input_query,strlen(input_query));
67 }
68
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");
72
73 return false;
74 }
75
76 bool HttpHandler::validate(string &default_schema,string &default_table,bool allow_drop_table)
77 {
78
79 if(not _schema || strcmp(_schema, "") == 0)
80 {
81 _schema = default_schema.c_str();
82 }
83 if(not _table || strcmp(_table,"")==0)
84 {
85 _table= default_table.c_str();
86 }
87 if(not _table || strcmp(_table,"")==0)
88 {
90 return true;
91 }
92
93 // Parse query object from json doc
94 Json::Features json_conf;
95 Json::Reader reader(json_conf);
96 bool retval = reader.parse(_query,_json_in);
97 if (retval != true)
98 {
99 _json_out["error_type"]="json error";
100 _json_out["error_message"]= reader.getFormattedErrorMessages();
101 }
102
103 // If _id was given as a URI parameter, copy the value to query object now.
104 // jsoncpp 1.7+ throws Json::LogicError from Value::asBool() when the value
105 // is a non-empty string; the older 0.6 release silently treated it as a
106 // falsy value. Decide "no usable _id from the body" with type-safe
107 // predicates instead.
108 {
109 const Json::Value& body_id= _json_in["query"]["_id"];
110 bool body_id_missing= body_id.isNull() ||
111 (body_id.isString() && body_id.asString().empty());
112 if (body_id_missing && _id)
113 {
114 _json_in["query"]["_id"] = (Json::Value::UInt) atol(_id);
115 }
116 }
117 // Check parameters from URI string. asString() is only safe on a
118 // string value in jsoncpp 0.6 — an _id of any other type is, by
119 // definition, present and non-empty.
120 const Json::Value &request_id= _json_in["query"]["_id"];
121 bool id_missing= request_id.isNull() ||
122 (request_id.isString() && request_id.asString().empty());
123 if(id_missing && _req->type==EVHTTP_REQ_DELETE && !allow_drop_table)
124 {
126 return true;
127 }
128 return !retval;
129 }
130
132 {
133 _json_out["error_type"]="http error";
134 _json_out["error_message"]= "table must be specified in URI query string.";
135 _http_response_code = HTTP_NOTFOUND;
136 _http_response_text = "table must be specified in URI query string.";
137 }
138
140 {
141 _json_out["error_type"]="http error";
142 _json_out["error_message"]= "_id must be specified in URI query string or set --json_server.allow_drop_table =true";
143 _http_response_code= HTTP_NOTFOUND;
144 _http_response_text= "_id must be specified in URI query string or set --json_server.allow_drop_table =true";
145 }
146
148 {
149 struct evbuffer *buf = evbuffer_new();
150 if(buf == NULL)
151 {
152 return;
153 }
154 Json::StyledWriter writer;
155 std::string output= writer.write(_json_out);
156 evbuffer_add(buf, output.c_str(), output.length());
157 evhttp_send_reply( _req, _http_response_code, _http_response_text, buf);
158 }
159
160}
161}
HttpHandler(Json::Value &json_out, Json::Value json_in, struct evhttp_request *req)
bool validate(string &default_schema, string &default_table, bool allow_drop_table)
STL namespace.