virtual void GetResponseHeaders(CefRefPtr<CefResponse> response, int64& response_length, CefString& redirectUrl)
	{
		response->SetMimeType(mimeType_);

		if (file_ == -1)
		{
			response->SetStatus(404);
		}
		else
		{
			response->SetStatus(200);
		}

		CefResponse::HeaderMap map;
		response->GetHeaderMap(map);

		map.insert(std::make_pair("cache-control", "no-cache, must-revalidate"));
		response->SetHeaderMap(map);

		if (file_ != -1)
		{
			response_length = device_->fileLength(file_);
		}
		else
		{
			response_length = 0;
		}
	}
JNIEXPORT void JNICALL Java_org_cef_network_CefResponse_1N_N_1SetHeaderMap
  (JNIEnv *env, jobject obj, jobject jheaderMap) {
  CefRefPtr<CefResponse> response = GetCefFromJNIObject<CefResponse>(env, obj, "CefResponse");
  if (!response)
    return;

  // public abstract java.util.Set<java.util.Map$Entry<K, V>> entrySet();
  jobject entrySet = NULL;
  JNI_CALL_METHOD(env, jheaderMap,
                  "entrySet",
                  "()Ljava/util/Set;",
                  Object,
                  entrySet);
  if (!entrySet)
    return;

  // public abstract java.lang.Object[] toArray();
  jobject entrySetValues = NULL;
  JNI_CALL_METHOD(env, entrySet,
                  "toArray",
                  "()[Ljava/lang/Object;",
                  Object,
                  entrySetValues);
  if (!entrySetValues)
    return;

  CefResponse::HeaderMap headerMap;
  jint length = env->GetArrayLength((jobjectArray)entrySetValues);
  for (jint i=0; i < length; i++) {
    jobject mapEntry = env->GetObjectArrayElement((jobjectArray)entrySetValues, i);
    if (!mapEntry)
      return;
    jobject key = NULL;
    jobject value = NULL;
    JNI_CALL_METHOD(env, mapEntry,
                    "getKey",
                    "()Ljava/lang/Object;",
                    Object,
                    key);
    JNI_CALL_METHOD(env, mapEntry,
                    "getValue",
                    "()Ljava/lang/Object;",
                    Object,
                    value);
    headerMap.insert(std::make_pair(GetJNIString(env, (jstring)key), GetJNIString(env, (jstring)value)));
  }
  response->SetHeaderMap(headerMap);
}
Beispiel #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;
    }