Beispiel #1
0
/**
 * Called whenever a new request is made. This sets up HttpRequest &
 * HttpResponse objects and then calls DispatchRequest.
 */
static int HandleRequest(void *http_server_ptr,
                         struct MHD_Connection *connection,
                         const char *url,
                         const char *method,
                         const char *version,
                         const char *upload_data,
                         size_t *upload_data_size,
                         void **ptr) {
  HttpServer *http_server = static_cast<HttpServer*>(http_server_ptr);
  HttpRequest *request;

  // on the first call ptr is null
  if (*ptr == NULL) {
    request = new HttpRequest(url, method, version, connection);
    if (!request)
      return MHD_NO;

    if (!request->Init()) {
      delete request;
      return MHD_NO;
    }
    *ptr = static_cast<void*>(request);
    return MHD_YES;
  }

  request = static_cast<HttpRequest*>(*ptr);

  if (request->InFlight())
    // don't dispatch more than once
    return MHD_YES;

  if (request->Method() == MHD_HTTP_METHOD_GET) {
    HttpResponse *response = new HttpResponse(connection);
    request->SetInFlight();
    return http_server->DispatchRequest(request, response);

  } else if (request->Method() == MHD_HTTP_METHOD_POST) {
    if (*upload_data_size != 0) {
      request->ProcessPostData(upload_data, upload_data_size);
      *upload_data_size = 0;
      return MHD_YES;
    }
    request->SetInFlight();
    HttpResponse *response = new HttpResponse(connection);
    return http_server->DispatchRequest(request, response);
  }
  return MHD_NO;
}
void jsWindow::load( const string& url )
{
    string to_load = url;
    string refer = location->url.tostring();
    int relocs = 0;

    HttpResponse* hresp;
    i_response_ptr resp;
    while (true) {
        HttpRequest* hreq = new HttpRequest(to_load);
        hreq->Method(HttpRequest::wemGet);
        hreq->depth(relocs);
        hreq->SetReferer(refer);
        i_request_ptr req(hreq);
        resp = browser->http_request(req);
        hresp = (HttpResponse*)resp.get();
        if (hresp->HttpCode() < 300 || hresp->HttpCode() >= 400) {
            break;
        }
        // process redirects
        if (relocs < 5) {
            relocs++;
            refer = to_load;
            string to_load = hresp->Headers().find_first("Location");
            if (to_load.empty()) {
                break;
            }
            // delete response
            resp.reset();
        } // if relocs < 5
        // delete request
        req.reset();
    } // while relocations
    if (hresp->HttpCode() < 300) {
        boost::shared_ptr<html_document> doc(new html_document());
        if(doc) {
            doc->ParseData(resp);
        // execute scripts
#ifdef V8_DOMSHELL
        string source;
            assign_document( doc );

            size_t i = 0;
            for(v8_wrapper::iterator_dfs it = document->begin_dfs(); it != document->end_dfs(); ++it) {
                if((*it)->m_tag == HTML_TAG_script && (*it)->m_entity) {
                    document->v8_wrapper::Registrator<v8_wrapper::jsDocument>::m_data.m_execution_point = *it;
                    source = (*it)->m_entity->attr("#code");
                    std::string src_url = (*it)->m_entity->attr("src");
                    if(src_url != "" && source == "") {
                        if(src_url.find(':') == -1) {
                            src_url = refer.substr(0, refer.rfind('/') + 1) + src_url;
                        }
                        HttpResponse* hresp_;
                        i_response_ptr resp_;
                        while (true) {
                            HttpRequest* hreq_ = new HttpRequest(src_url);
                            hreq_->Method(HttpRequest::wemGet);
                            hreq_->depth(relocs);
                            hreq_->SetReferer(refer);
                            i_request_ptr req_(hreq_);
                            resp_ = browser->http_request(req_);
                            hresp_ = (HttpResponse*)resp_.get();
                            if (hresp_->HttpCode() < 300 || hresp_->HttpCode() >= 400) {
                                break;
                            }
                        }
                        if (hresp_->HttpCode() < 300) {
                            source.assign((const char*)&resp_->Data()[0], resp_->Data().size());
                            (*it)->m_entity->attr("#code", source);
                        }
                    }
#ifdef _DEBUG
                    LOG4CXX_TRACE(webEngine::iLogger::GetLogger(), _T("audit_jscript::parse_scripts execute script #") << i++ << "; Source:\n" << source);
#endif
                browser->execute_string(source, "", true, true);
            }
        }

            process_events(browser, doc, false);
#endif
        }
    } else {
        LOG4CXX_WARN(webEngine::iLogger::GetLogger(), _T("jsWindow::load the ") << hresp->RealUrl().tostring() << _T(" failed! HTTP code=") << hresp->HttpCode());
    }
}