Ejemplo n.º 1
0
/**
 * Display the 404 Not Found page.
 */
void PageHandler::not_found(struct evhttp_request* request, void* page_handler_ptr) {
    PageHandler* this_ = (PageHandler*) page_handler_ptr;
    std::string uri = request_uri_path(request);
    
    //Compose the not found page.
    shared_array<char> escaped_uri(evhttp_htmlescape(uri.c_str()));
    const int page_size = 
        sprintf(this_->page_buffer_.get(), this_->not_found_template_.c_str(), escaped_uri.get());
    const size_t u_page_size = static_cast<size_t>(page_size);
    
    response_cache_public(request, 3600 /* sec */);
    this_->server_->send_response_data(request, 
                                       this_->page_buffer_.get(), 
                                       u_page_size, 
                                       HTTP_OK);
}
void
SearchapiServiceClientEvhttp::searchHandler( struct evhttp_request *req, 
                                             void *arg )
{
  SearchapiServiceClientEvhttp *instance = static_cast<SearchapiServiceClientEvhttp*>( arg );
  struct evbuffer *buf;
  std::string result;

  buf = evbuffer_new();
  if (buf) {
    struct evkeyvalq headers;
    TAILQ_INIT(&headers);
    std::cerr << std::string(req->uri) << std::endl;
    evhttp_parse_query(req->uri, &headers);

    std::string key = "q";
    std::string val = "";

    if (BBRpcClientEvhttp::find_header(&headers, key, val))
    {
      char *escaped_val = evhttp_htmlescape(val.c_str());
      std::cerr << escaped_val << std::endl;
      result = instance->search(escaped_val);
      evhttp_add_header(req->output_headers, "Content-type","application/x-javascript; charset=utf-8");
      std::stringstream result_size;
      result_size << result.size();
      evhttp_add_header(req->output_headers, "Content-Length", result_size.str().c_str());
      evbuffer_add_printf(buf, "%s", result.c_str());
      free(escaped_val);
    }
    else
    {
      std::cerr << "not find" << std::endl;
    }
    evhttp_clear_headers(&headers);
    evhttp_send_reply(req, HTTP_OK, "OK", buf);
  }
  else
  {
    std::cerr << "failed to create response buffer" << std::endl;
  }
}