void
dynamic_1B(TestArg *arg)
{
    char *uri;
    HttpEngine engine;
    HttpRequest *request;
    HttpResponse *response;

    arg->returnVal = 0;

    uri = (char *)malloc(sizeof(TEST_QUERY) + strlen(arg->uri) + 2);
    sprintf(uri, "%s?%s", arg->uri, TEST_QUERY);

    request = new HttpRequest(arg->server, uri, arg->proto);
    request->setExpectDynamicBody();
    response = engine.makeRequest(*request);

    if (response->getStatus() != 200) {
        Logger::logError(LOGERROR, "server responded with status code %d", response->getStatus());
        arg->returnVal = -1;
    }

    char *query = response->getDynamicResponse().lookupValue("BASIC", "queryString");
    if (!query || strcmp(query, TEST_QUERY)) {
        Logger::logError(LOGERROR, "query string mismatch! (\"%s\")", query);
        arg->returnVal = -1;
    }

    free(uri);
    delete request;
    delete response;
}
void
dynamic_1D(TestArg *arg)
{
    HttpEngine engine;
    HttpRequest request(arg->server, arg->uri, arg->proto);
    HttpResponse *response;
    char header[1024], value[1024];

    arg->returnVal = 0;

    memset(header, 'X', 1024);
    header[1024-1] = '\0';
    memset(value, 'Y', 1024);
    value[1024-1] = '\0';

    request.addHeader(header,value);
    request.setExpectDynamicBody();
    response = engine.makeRequest(request);

    if (response->getStatus() != 200) {
        Logger::logError(LOGERROR, "server responded with status code %d", response->getStatus());
        arg->returnVal = -1;
    }

    char *rvheader = response->getDynamicResponse().lookupValue("REQUEST HEADERS", header);
    if (!rvheader || strcmp(rvheader, value)) {
fprintf(stdout, "strlen(rvheader) is %d\n", strlen(rvheader));
fprintf(stdout, "strlen(header) is %d\n", strlen(header));
        Logger::logError(LOGERROR, "long header mismatch! (\"%s\")", rvheader);
        arg->returnVal = -1;
    }

    delete response;
}
bool RPCTestNode::startMining(size_t threadsCount, const std::string& address) { 
  LOG_DEBUG("startMining()");
  using namespace cryptonote;
  COMMAND_RPC_START_MINING::request req;
  COMMAND_RPC_START_MINING::response resp;
  req.miner_address = address;
  req.threads_count = threadsCount;
  std::stringstream requestStream;
  JsonOutputStreamSerializer enumerator;
  enumerator(req, "");
  requestStream << enumerator;
  HttpRequest httpReq;
  prepareRequest(httpReq, "/start_mining", requestStream.str());
  HttpResponse httpResp;
  sendRequest(httpReq, httpResp);
  if (httpResp.getStatus() != HttpResponse::STATUS_200) return false;
  std::stringstream responseStream(httpResp.getBody());
  JsonInputStreamSerializer en(responseStream);
  en(resp, "");
  if (resp.status != CORE_RPC_STATUS_OK) {
    std::cout << "startMining() RPC call fail: " << resp.status;
    return false;
  }

  return true;
}
void
dynamic_1C(TestArg *arg)
{
    HttpEngine engine;
    HttpRequest request(arg->server, arg->uri, arg->proto);
    HttpResponse *response;

    arg->returnVal = 0;

    request.addHeader(HEADER1, VALUE1);
    request.addHeader(HEADER2, VALUE2);
    request.addHeader(HEADER3, VALUE3);
    request.addHeader(HEADER4, VALUE4);
    request.setExpectDynamicBody();
    response = engine.makeRequest(request);

    if (response->getStatus() != 200) {
        Logger::logError(LOGERROR, "server responded with status code %d", response->getStatus());
        arg->returnVal = -1;
    }

    char *header = response->getDynamicResponse().lookupValue("REQUEST HEADERS", HEADER1);
    if (!header || strcmp(header, VALUE1)) {
        Logger::logError(LOGERROR, "header1 mismatch! (\"%s\")", header);
        arg->returnVal = -1;
    }

    header = response->getDynamicResponse().lookupValue("REQUEST HEADERS", HEADER2);
    if (!header || strcmp(header, VALUE2)) {
        Logger::logError(LOGERROR, "header2 mismatch! (\"%s\")", header);
        arg->returnVal = -1;
    }

    header = response->getDynamicResponse().lookupValue("REQUEST HEADERS", HEADER3);
    if (!header || strcmp(header, VALUE3)) {
        Logger::logError(LOGERROR, "header3 mismatch! (\"%s\")", header);
        arg->returnVal = -1;
    }

    header = response->getDynamicResponse().lookupValue("REQUEST HEADERS", HEADER4);
    if (!header || strcmp(header, VALUE4)) {
        Logger::logError(LOGERROR, "header4 mismatch! (\"%s\")", header);
        arg->returnVal = -1;
    }

    delete response;
}
/**
 * @brief PathDelegator::service -- Delegate the request to the service designed to handle that request
 * @param request
 * @param response
 */
void PathDelegator::service(HttpRequest &request, HttpResponse &response) {
    QByteArray path = removeExtension(request.getPath());
    if (paths.contains(path)) {
        paths[path]->service(request, response);
    }
    else {
        response.setStatus(HttpHeaders::STATUS_NOT_FOUND, QByteArray("Cannot find ") + path);
    }
    WebLogger::log(QtWarningMsg, QByteArray::number(response.getStatus()) + ": " + response.getStatusText(), "pathdelegator", "service");
}
void
dynamic_1A(TestArg *arg)
{
    HttpEngine engine;
    HttpRequest request(arg->server, arg->uri, arg->proto);
    HttpResponse *response;

    arg->returnVal = 0;

    request.setExpectDynamicBody();
    response = engine.makeRequest(request);

    if (response->getStatus() != 200) {
        Logger::logError(LOGERROR, "server responded with status code %d", response->getStatus());
        arg->returnVal = -1;
    }

    delete response;
}
示例#7
0
void invokeJsonRpcCommand(HttpClient& httpClient, JsonRpcRequest& jsReq, JsonRpcResponse& jsRes) {
  HttpRequest httpReq;
  HttpResponse httpRes;

  httpReq.setUrl("/json_rpc");
  httpReq.setBody(jsReq.getBody());

  httpClient.request(httpReq, httpRes);

  if (httpRes.getStatus() != HttpResponse::STATUS_200) {
    throw std::runtime_error("JSON-RPC call failed, HTTP status = " + std::to_string(httpRes.getStatus()));
  }

  jsRes.parse(httpRes.getBody());

  JsonRpcError err;
  if (jsRes.getError(err)) {
    throw err;
  }
}
bool RPCTestNode::submitBlock(const std::string& block) {
  HttpRequest httpReq;
  httpReq.setUrl("/json_rpc");
  httpReq.addHeader("Host", "127.0.0.1:" + boost::lexical_cast<std::string>(m_rpcPort));
  httpReq.addHeader("Content-Type", "application/json-rpc");
  JsonValue request(cryptonote::JsonValue::OBJECT);
  JsonValue jsonRpc;
  jsonRpc = "2.0";
  request.insert("jsonrpc", jsonRpc);
  JsonValue methodString;
  methodString = "submitblock";
  request.insert("method", methodString);
  JsonValue id;
  id = "sync";
  request.insert("id", id);
  JsonValue params(JsonValue::ARRAY);
  JsonValue blockstr;
  blockstr = block.c_str();
  params.pushBack(blockstr);
  request.insert("params", params);
  std::stringstream jsonOutputStream;
  jsonOutputStream << request;
  httpReq.setBody(jsonOutputStream.str());
  TcpConnector connector(m_dispatcher, "127.0.0.1", m_rpcPort);
  TcpConnection connection = connector.connect();
  TcpStreambuf streambuf(connection);
  std::iostream connectionStream(&streambuf);
  LOG_DEBUG("invoke json-rpc: " + httpReq.getBody());
  connectionStream << httpReq;
  connectionStream.flush();
  HttpResponse httpResp;
  HttpParser parser;
  parser.receiveResponse(connectionStream, httpResp);
  connectionStream.flush();
  if (httpResp.getStatus() != HttpResponse::STATUS_200) return false;

  epee::serialization::portable_storage ps;
  if (!ps.load_from_json(httpResp.getBody())) {
    LOG_ERROR("cannot parse response from daemon: " + httpResp.getBody());
    return false;
  }

  epee::json_rpc::response<COMMAND_RPC_SUBMITBLOCK::response, epee::json_rpc::error> jsonRpcResponse;
  jsonRpcResponse.load(ps);

  if (jsonRpcResponse.error.code || jsonRpcResponse.error.message.size()) {
    LOG_ERROR("RPC call of submit_block returned error: " + TO_STRING(jsonRpcResponse.error.code) + ", message: " + jsonRpcResponse.error.message);
    return false;
  }
   
  if (jsonRpcResponse.result.status != CORE_RPC_STATUS_OK)  return false;
  return true;
}
bool RPCTestNode::stopDaemon() {
  LOG_DEBUG("stopDaemon()");
  using namespace cryptonote;
  COMMAND_RPC_STOP_DAEMON::request req;
  COMMAND_RPC_STOP_DAEMON::response resp;
  std::stringstream requestStream;
  JsonOutputStreamSerializer enumerator;
  enumerator(req, "");
  requestStream << enumerator;
  HttpRequest httpReq;
  prepareRequest(httpReq, "/stop_daemon", requestStream.str());
  HttpResponse httpResp;
  sendRequest(httpReq, httpResp);
  if (httpResp.getStatus() != HttpResponse::STATUS_200) return false;
  std::stringstream responseStream(httpResp.getBody());
  JsonInputStreamSerializer en(responseStream);
  en(resp, "");
  if (resp.status != CORE_RPC_STATUS_OK) {
    std::cout << "stopDaemon() RPC call fail: " << resp.status;
    return false;
  }

  return true;
}
示例#10
0
void processResponse(HttpResponse resp){
  if(resp.getStatus() == HttpResponse::OK_200){

  }
}
示例#11
0
void do_child(int fd, char* dir){
    int recvLen = 0;
    int p_size = 0;
    std::vector<char> pload;
    char buf[500] = {0};
    memset(buf, '\0', sizeof(buf));
    
    char filepath[100];
    
    struct timeval tv;
    tv.tv_sec = 40;
    tv.tv_usec = 0;
    setsockopt (fd, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(struct timeval));
    std::vector <char> wire;
    std::vector <char> res_wire;
    
    while (true){
    strcpy(filepath, dir);
    bool isdone = false;
    struct timeval tv;
    tv.tv_sec = 30;
    tv.tv_usec = 0;
    setsockopt (fd, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(struct timeval));
    while(1){
        memset(buf, '\0', sizeof(buf));
        if ((recvLen = recv(fd, buf, 500, 0)) == -1) {
            std::cout << "Timeout expires" << std::endl;
            perror("recv");
            close(fd);
            return;
        }
        for (int i =0; i < recvLen; i++)
        {
            wire.push_back(buf[i]);
            if(buf[i] == '\r' && buf[i+1] == '\n' && buf[i+2] == '\r' && buf[i+3] == '\n')
            {
                wire.push_back(buf[i+1]);
                wire.push_back(buf[i+2]);
                wire.push_back(buf[i+3]);
                isdone = true;
                break;
            }
        }
        if(isdone == true)
            break;
    }
        
        std::cout << "Received the request message: "<<std::endl;
        for (int i = 0; i<wire.size(); i++) {
            std::cout << wire[i];
        }
        std::cout << std::endl;
        
        HttpRequest request;
        request.decodeFirstLine(wire);
        request.decodeHeaderline(wire);
        wire.clear();
        HttpResponse response;
        response.setVersion(request.getVersion());
        HttpStatus status;
        bool set_status = false;
    
        if(request.getMap("Connection") == "Keep-Alive")
            response.setHeader("Connection", "Keep-Alive");
        else if(request.getMap("Connection") == "Close")
            response.setHeader("Connection", "Close");
        else
        {
            if(set_status == false){
            status.m_statuscode = "400";
            response.setStatus(status);
            response.setDescription ("Bad Request");
            response.setHeader("Content-Length", "0");
            set_status = true;
            }
        }
    
        std::string recurl = request.getUrl();

        int len = recurl.length();
        int i = 0;
    
        if(recurl[i] == 'h'&& recurl[i+1] == 't' && recurl[i+2] == 't' && recurl[i+3] == 'p' &&recurl[i+4] ==':' && recurl[i+5]=='/' &&recurl[i+6] == '/')
        {
            i+=7;
            char host_check[200] = {'\0'};
            int pt_check = -1;
            int j = 0;
            while (i < len)
            {
                if(recurl[i]!= '/' && recurl[i]!= ':')
                {
                    host_check[j] = recurl[i];
                    j++;
                    i++;
                }
                else
                    break;
            }
            std::string mycheck = host_check;
            if(recurl[i] == ':')
            {
                j = 0;
                i++;
                char po_no[100];
                while (i < len){
                    if(recurl[i] == '/')
                        break;
                    po_no[j] = recurl[i];
                    i++;
                    j++;
                }
                pt_check = atoi(po_no);
            }
            if(mycheck != hostname || (portno != pt_check))
            {
                if(set_status == false){
                status.m_statuscode = "400";
                response.setStatus(status);
                response.setDescription ("Bad Request");
                response.setHeader("Content-Length", "0");
                set_status = true;
                }
            }
            else
                strcat(filepath, recurl.substr(i).c_str());
        }
        else
        {
            if(recurl[i] != '/')
            {
                if(set_status == false){
                status.m_statuscode = "400";
                response.setStatus(status);
                response.setDescription ("Bad Request");
                response.setHeader("Content-Length", "0");
                set_status = true;
                }
            }
            else
            {
                std::string hosturl = request.getMap("Host");
                if (hosturl == "Cannot find this key") {
                    if(set_status == false){
                    status.m_statuscode = "400";
                    response.setStatus(status);
                    response.setDescription ("Bad Request");
                    response.setHeader("Content-Length", "0");
                    set_status = true;
                    }
                }
                else
                {
                    char host_check[200] = {'\0'};
                    int pt_check = -1;
                    i = 0;
                    while (i < hosturl.length())
                    {
                        if(hosturl[i]!= '/' && hosturl[i]!= ':')
                        {
                            host_check[i] = hosturl[i];
                            i++;
                        }
                        else
                            break;
                    }
                    std::string mycheck = host_check;
                    if(hosturl[i] == ':')
                    {
                        //if we hit a colon, check for port number
                        int j = 0;
                        i++;
                        char po_no[100] = {'\0'};
                        while (i < hosturl.length()){
                            po_no[j] = hosturl[i];
                            i++;
                            j++;
                        }
                        pt_check = atoi(po_no);
                    }
                    if(mycheck != hostname || (portno != pt_check))
                    {
                        if(set_status == false){
                        status.m_statuscode = "400";
                        response.setStatus(status);
                        response.setDescription ("Bad Request");
                        response.setHeader("Content-Length", "0");
                        set_status = true;
                        }
                    }
                    else
                        strcat(filepath, recurl.c_str());

                }
            }
        }
    
        
    if(strcmp(request.getVersion().m_versionstr.c_str(), "HTTP/1.0" ) != 0 && strcmp(request.getVersion().m_versionstr.c_str(), "HTTP/1.1" ) != 0)
    {
        if(set_status == false){
        status.m_statuscode = "505";
        response.setStatus(status);
        response.setDescription ("HTTP version not supported");
        response.setHeader("Content-Length", "0");
        set_status = true;
        }
    }

    else if (request.getMethod().m_methodstr != "GET") {
        if(set_status == false){
        status.m_statuscode = "405";
        response.setStatus(status);
        response.setDescription ("Method not allowed");
        response.setHeader("Content-Length", "0");
        set_status = true;
        }
    }
    else if(set_status == false)
    {
        std::ifstream file;
        file.exceptions(
                        std::ifstream::badbit
                        | std::ifstream::eofbit);
        
        file.open(filepath, std::ifstream::in | std::ifstream::binary | std::ifstream::out);

        if (!file.is_open())
        {
            status.m_statuscode = "404";
            response.setStatus(status);
            response.setDescription ("Not Found");
            response.setHeader("Content-Length", "0");
            set_status = true;
        }
        else
        {
            status.m_statuscode = "200";
            response.setStatus(status);
            response.setDescription ("OK");
            set_status = true;
            file.seekg(0, std::ios::end);
            std::streampos length(file.tellg());
            if (length) {
                file.seekg(0, std::ios::beg);
                pload.resize(static_cast<std::size_t>(length));
                file.read(&pload.front(), static_cast<std::size_t>(length));
            }
            file.close();
            response.setPayLoad(pload);
            std::stringstream out;
            p_size = pload.size();
            out << p_size;
            response.setHeader("Content-Length", out.str());
        }
        memset(filepath, '\0', 100);
    }
    res_wire = response.encode();
    int wsize = res_wire.size();
    
    
    memset(buf, '\0', sizeof(buf));
    for (int k1 = 0; k1 < wsize; k1++) {
        buf[k1] = res_wire[k1];
    }
    
    res_wire.clear();
        
    if (send(fd, buf, wsize, 0) == -1) {
        perror("send");
        exit(1);
    }
    
    if(response.getStatus().m_statuscode == "200")
    {
        int divide = p_size/500;
        int remainder = p_size % 500;
        int count = 0;
        for (int k = 0; k < divide; k++) {
            memset(buf, '\0', sizeof(buf));
            for (int k1 = 0; k1 < 500; k1++) {
                buf[k1] = pload[count];
                count ++;
            }

            if (send(fd, buf, 500, 0) == -1) {
                perror("send");
                exit(1);
            }
        }
        if(remainder!=0)
        {
            memset(buf, '\0', sizeof(buf));
            for(int q = 0; q < remainder; q++)
            {
                buf[q] = pload[count];
                count++;
            }
           
            if (send(fd, buf, remainder, 0) == -1) {
                perror("send");
                exit(1);
            }
        }
    }
    pload.clear();
    if(response.getMap("Connection") == "close")
    {
        std::cout << "successfully close the connection" <<std::endl;
        close(fd);
        break;
    }
}

}