示例#1
0
void DumpRequestContents(CefRefPtr<CefRequest> request, std::string& str)
{
  std::stringstream ss;

  ss << "URL: " << std::string(request->GetURL());
  ss << "\nMethod: " << std::string(request->GetMethod());

  CefRequest::HeaderMap headerMap;
  request->GetHeaderMap(headerMap);
  if(headerMap.size() > 0) {
    ss << "\nHeaders:";
    CefRequest::HeaderMap::const_iterator it = headerMap.begin();
    for(; it != headerMap.end(); ++it) {
      ss << "\n\t" << std::string((*it).first) << ": " <<
          std::string((*it).second);
    }
  }

  CefRefPtr<CefPostData> postData = request->GetPostData();
  if(postData.get()) {
    CefPostData::ElementVector elements;
    postData->GetElements(elements);
    if(elements.size() > 0) {
      ss << "\nPost Data:";
      CefRefPtr<CefPostDataElement> element;
      CefPostData::ElementVector::const_iterator it = elements.begin();
      for(; it != elements.end(); ++it) {
        element = (*it);
        if(element->GetType() == PDE_TYPE_BYTES) {
          // the element is composed of bytes
          ss << "\n\tBytes: ";
          if(element->GetBytesCount() == 0)
            ss << "(empty)";
          else {
            // retrieve the data.
            size_t size = element->GetBytesCount();
            char* bytes = new char[size];
            element->GetBytes(size, bytes);
            ss << std::string(bytes, size);
            delete [] bytes;
          }
        } else if(element->GetType() == PDE_TYPE_FILE) {
          ss << "\n\tFile: " << std::string(element->GetFile());
        }
      }
    }
  }

  str = ss.str();
}
示例#2
0
// Implementation of the schema handler for appjs:// requests.
void AppjsSchemeHandler::Execute(CefThreadId threadId) {

  REQUIRE_UI_THREAD();

  HandleScope scope;

  Local<Object>  global = Context::GetCurrent()->Global();
  Local<Object> emitter = global->Get(String::NewSymbol("process"))->ToObject();

  const int argc = 3;

  Handle<Value>    self = WrapObject(this);
  Local<Function>    cb = FunctionTemplate::New(NodeCallback,self)->GetFunction();
  Local<Object>     req = Object::New();
  Local<String>    post = String::New("");
  Local<Object> headers = Object::New();
  Local<String>   files = String::New("");

  CefRequest::HeaderMap headerMap;
  request_->GetHeaderMap(headerMap);

  if (headerMap.size() > 0) {
    CefRequest::HeaderMap::const_iterator it = headerMap.begin();
    for ( ; it != headerMap.end(); ++it) {
      headers->Set(String::New((uint16_t*)(*it).first.c_str()),String::New((uint16_t*)(*it).second.c_str()));
    }
  }

  CefRefPtr<CefPostData> postData = request_->GetPostData();

  if(postData.get()){

    CefPostData::ElementVector elements;

    postData->GetElements(elements);

    if (elements.size() > 0) {
      CefRefPtr<CefPostDataElement> element;
      CefPostData::ElementVector::const_iterator it = elements.begin();

      for ( ; it != elements.end(); ++it) {
        element = (*it);
        if (element->GetType() == PDE_TYPE_BYTES && element->GetBytesCount()) {

          // retrieve the data.
          size_t size = element->GetBytesCount();
          char* bytes = new char[size];
          element->GetBytes(size, bytes);
          post = String::New(bytes,size);
          delete [] bytes;

        } else if (element->GetType() == PDE_TYPE_FILE) {
          //TODO Needs testing
          files = String::New((uint16_t*)element->GetFile().c_str());
        }
      }

    }
  }

  Handle<Value> method = String::New((uint16_t*)request_->GetMethod().c_str());
  Handle<Value> url = String::New((uint16_t*)request_->GetURL().c_str());

  req->Set(String::NewSymbol("method"),method);
  req->Set(String::NewSymbol("url"),url);
  req->Set(String::NewSymbol("post"),post);
  req->Set(String::NewSymbol("headers"),headers);
  req->Set(String::NewSymbol("files"),files);

  Handle<Value> argv[argc] = {String::New("appjs-request"),req,cb};
  node::MakeCallback(emitter,"emit",argc,argv);

}
示例#3
0
    CefRefPtr<CefResourceHandler> RequestHandler::GetResourceHandler(CefRefPtr<CefBrowser> browser,
                                                                     CefRefPtr<CefFrame> frame,
                                                                     CefRefPtr<CefRequest> request) {
        std::string url = request->GetURL().ToString();
        std::string method = request->GetMethod().ToString();

        int split_index = url.find_first_of('?');
        std::string base_url;
        std::string get_params;
        if (split_index == -1) {
            base_url = url;
        } else {
            base_url = url.substr(0, split_index);
            get_params = url.substr(split_index + 1);
        }

        bool found = false;
        Route *route = nullptr;
        std::vector<Route *>::iterator route_iter = routes.begin();
        while (route_iter != routes.end() && !found) {
            route = (*route_iter);
            std::smatch match;
            if (std::regex_search(base_url, match, route->RouteRegex())) {
                found = true;
            }
            ++route_iter;
        }

        if (found) {
            std::vector<std::pair<std::string, std::string> > get;
            std::vector<std::string> get_param_chunks = split(get_params, '&');
            std::vector<std::string>::iterator get_param_chunk_iter = get_param_chunks.begin();
            while (get_param_chunk_iter != get_param_chunks.end()) {
                std::string chunk = (*get_param_chunk_iter);
                std::string name = chunk.substr(0, chunk.find_first_of('='));
                std::string value = chunk.substr(chunk.find_first_of('='));
                get.push_back(std::pair<std::string, std::string>(name, value));
                ++get_param_chunk_iter;
            }

            std::vector<std::pair<std::string, std::string> > post;
            CefRefPtr<CefPostData> post_data = request->GetPostData();
            if (post_data.get() != nullptr) {
                CefPostData::ElementVector post_elements;
                post_data->GetElements(post_elements);
                CefPostData::ElementVector::iterator post_elem_iter = post_elements.begin();
                while (post_elem_iter != post_elements.end()) {
                    std::string element_data;
                    std::string element_type;
                    CefRefPtr<CefPostDataElement> element = (*post_elem_iter);
                    if (element->GetType() == PDE_TYPE_EMPTY) {
                        element_type = "empty";
                    } else if (element->GetType() == PDE_TYPE_BYTES) {
                        element_data = ReadPostElementBytes(element);
                        element_type = "string";
                    } else if (element->GetType() == PDE_TYPE_FILE) {
                        element_data = element->GetFile().ToString();
                        element_type = "file";
                    }
                    post.push_back(std::pair<std::string, std::string>(element_type, element_data));
                    ++post_elem_iter;
                }
            }

            Response *result = route->Call(base_url, method, get, post);
            if (result == nullptr) {
                return nullptr;
            }

            std::string content_str = result->GetContent();
            int content_size = content_str.size();
            const char *content = content_str.c_str();

            CefRefPtr<CefStreamReader> result_stream = CefStreamReader::CreateForData(
                                                           static_cast<void*>(const_cast<char*>(content)), content_size);
            CefResponse::HeaderMap headers;
            headers.insert(std::pair<CefString, CefString>("Access-Control-Allow-Origin", "*"));
            headers.insert(std::pair<CefString, CefString>("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"));
            headers.insert(std::pair<CefString, CefString>("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, PATCH, DELETE"));

            return new CefStreamResourceHandler(200, "200 OK", result->GetMimeType(), headers, result_stream);
        }

        return nullptr;
    }