Drizzled Public API Documentation

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 
25 using namespace std;
26 
27 namespace drizzle_plugin
28 {
29 namespace 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;
38  _http_response_text="OK";
39  _json_out=json_out;
40  _json_in=json_in;
41  _req=req;
42  }
43 
44  bool HttpHandler::handleRequest()
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  {
89  generateHttpError();
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.getFormatedErrorMessages();
101  }
102 
103  // If _id was given as a URI parameter, copy the value to query object now.
104  if ( !_json_in["query"]["_id"].asBool() )
105  {
106  if( _id )
107  {
108  _json_in["query"]["_id"] = (Json::Value::UInt) atol(_id);
109  }
110  }
111  // Check parameters from URI string
112  if((_json_in["query"]["_id"].isNull() || _json_in["query"]["_id"].asString()=="") && _req->type==EVHTTP_REQ_DELETE && !allow_drop_table)
113  {
114  generateDropTableError();
115  return true;
116  }
117  return !retval;
118  }
119 
120  void HttpHandler::generateHttpError()
121  {
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.";
126  }
127 
128  void HttpHandler::generateDropTableError()
129  {
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";
134  }
135 
136  void HttpHandler::sendResponse()
137  {
138  struct evbuffer *buf = evbuffer_new();
139  if(buf == NULL)
140  {
141  return;
142  }
143  Json::StyledWriter writer;
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);
147  }
148 
149 }
150 }