Beispiel #1
0
INetResponse* CURLNetRequest::pushMultipartData(const String& strUrl, VectorPtr<CMultipartItem*>& arItems, IRhoSession* oSession, Hashtable<String,String>* pHeaders)
{
    int nRespCode = -1;
    String strRespBody;
    
    RAWLOG_INFO1("POST request (Push): %s", strUrl.c_str());
    rho_net_impl_network_indicator(1);
    
    curl_slist *hdrs = m_curl.set_options("POST", strUrl, String(), oSession, pHeaders);
    CURL *curl = m_curl.curl();
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strRespBody);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curlBodyStringCallback);
	
    curl_httppost *post = NULL, *last = NULL;
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL);

    for (size_t i = 0, lim = arItems.size(); i < lim; ++i) {
        CMultipartItem *mi = arItems[i];
        
        size_t cl;
        if (mi->m_strFilePath.empty())
            cl = mi->m_strBody.size();
        else {
            common::CRhoFile f;
            if (!f.open(mi->m_strFilePath.c_str(), common::CRhoFile::OpenReadOnly))
                cl = 0;
            else {
                cl = f.size();
                f.close();
            }
        }

        char buf[32];
        buf[sizeof(buf) - 1] = '\0';
        snprintf(buf, sizeof(buf) - 1, "Content-Length: %lu", (unsigned long)cl);
        curl_slist *fh = NULL;
        fh = curl_slist_append(fh, buf);

        const char *name = mi->m_strName.empty() ? "blob" : mi->m_strName.c_str();
        int opt = mi->m_strFilePath.empty() ? CURLFORM_COPYCONTENTS : CURLFORM_FILE;
        const char *data = mi->m_strFilePath.empty() ? mi->m_strBody.c_str() : mi->m_strFilePath.c_str();
        const char *ct = mi->m_strContentType.empty() ? NULL : mi->m_strContentType.c_str();
        if (ct) {
            curl_formadd(&post, &last,
                         CURLFORM_COPYNAME, name,
                         opt, data,
                         CURLFORM_CONTENTTYPE, ct,
                         CURLFORM_CONTENTHEADER, fh,
                         CURLFORM_END);
        }
        else {
            curl_formadd(&post, &last,
                         CURLFORM_COPYNAME, name,
                         opt, data,
                         CURLFORM_CONTENTHEADER, fh,
                         CURLFORM_END);
        }
    }
        
    curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
    
    CURLcode err = doCURLPerform(strUrl);
	
    curl_slist_free_all(hdrs);
    curl_formfree(post);
    
    rho_net_impl_network_indicator(0);
    
    nRespCode = getResponseCode(err, strRespBody, oSession);
    return makeResponse(strRespBody, nRespCode);
}
Beispiel #2
0
INetResponse *CURLNetRequest::makeResponse(String const &body, int nErrorCode)
{
    return makeResponse(body.c_str(), body.size(), nErrorCode);
}
Beispiel #3
0
INetResponse* CURLNetRequest::doPull(const char* method, const String& strUrl,
                                     const String& strBody, common::CRhoFile *oFile,
                                     IRhoSession* oSession, Hashtable<String,String>* pHeaders )
{
    int nRespCode = -1;
    Vector<char> respBody;
    long nStartFrom = 0;
    if (oFile)
        nStartFrom = oFile->size();

	if( !net::URI::isLocalHost(strUrl.c_str()) )
    {
        //Log every non localhost requests
        RAWLOG_INFO2("%s request (Pull): %s", method, strUrl.c_str());
        rho_net_impl_network_indicator(1);
    }

    Hashtable<String,String> h;
    if (pHeaders)
        h = *pHeaders;
    
    for (int nAttempts = 0; nAttempts < 10; ++nAttempts) {
        Vector<char> respChunk;
        
        curl_slist *hdrs = m_curl.set_options(method, strUrl, strBody, oSession, &h);
        CURL *curl = m_curl.curl();
        if (pHeaders) {
            curl_easy_setopt(curl, CURLOPT_HEADERDATA, pHeaders);
            curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, &curlHeaderCallback);
        }
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &respChunk);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curlBodyBinaryCallback);
        if (nStartFrom > 0)
            curl_easy_setopt(curl, CURLOPT_RESUME_FROM, nStartFrom);

        CURLcode err = doCURLPerform(strUrl);
        curl_slist_free_all(hdrs);
        
        long statusCode = 0;
        if (curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &statusCode) != 0)
            statusCode = 500;
		
		if (statusCode == 416 )
		{
			//Do nothing, file is already loaded
		}else if (statusCode == 206) {
            if (oFile)
                oFile->write(&respChunk[0], respChunk.size());
            else
                std::copy(respChunk.begin(), respChunk.end(), std::back_inserter(respBody));
            // Clear counter of attempts because 206 response does not considered to be failed attempt
            nAttempts = 0;
        }
        else {
            if (oFile) {
                oFile->movePosToStart();
                oFile->write(&respChunk[0], respChunk.size());
            }
            else
                respBody = respChunk;
        }
        
        if (err == CURLE_OPERATION_TIMEDOUT && respChunk.size() > 0) {
            RAWTRACE("Connection was closed by timeout, but we have part of data received; try to restore connection");
            nStartFrom = oFile ? oFile->size() : respBody.size();
            continue;
        }
        
        nRespCode = getResponseCode(err, respBody, oSession);
        break;
    }

	if( !net::URI::isLocalHost(strUrl.c_str()) )		   
	   rho_net_impl_network_indicator(0);

    return makeResponse(respBody, nRespCode);
}
Beispiel #4
0
INetResponse *CURLNetRequest::makeResponse(Vector<char> const &body, int nErrorCode)
{
    return makeResponse(&body[0], body.size(), nErrorCode);
}
Beispiel #5
0
bool Engine::parseFile(FILE *file,HttpServerRequest &httpRequest,std::string &script)
{
	std::string content;

	char *buf = new char[IO_BUFFER_SIZE+1];
	memset(buf,0,IO_BUFFER_SIZE);

	size_t bytesRead = 0;
	while ( (bytesRead = fread(buf,1,IO_BUFFER_SIZE,file))>0 ) {
		content.append(std::string(buf,bytesRead));
		memset(buf,0,IO_BUFFER_SIZE);
	}

	delete[] buf;

	std::string directive;
	std::string text;

	size_t pos = 0;
	size_t start = 0;
	size_t end = 0;

	int lineCount = 0;

	std::string::const_iterator iter = content.begin();
	while ( iter!=content.end() )
	{
		if ( *iter=='<' )
		{
			if ( *++iter=='?' )
			{
				if ( *++iter=='@' ) {
					directive.push_back(*++iter);
				}

				if ( !text.empty() )
				{
					script.append(makeResponse(text));
					for ( int i=0; i<lineCount; i++ ) {
						script.append("\r\n");
					}

					text.clear();
					lineCount = 0;
				}

				while ( iter!=content.end() )
				{
					if ( *iter=='?' )
					{
						if ( *++iter=='>' )
						{
							if ( !directive.empty() ) 
							{
								std::string output;
								if ( executeDirective(directive,httpRequest,output) ) {
									script.append(output);
									directive.clear();
								}
								else {
									return false;
								}
							}

							iter++;
							break;
						}
						else 
						{
							if ( !directive.empty() ) {
								directive.push_back('?');
							}
							else {
								script.push_back('?');
							}
						}
					}

					if ( !directive.empty() ) {
						directive.push_back(*iter++);
					}
					else {
						script.push_back(*iter++);
					}
				}

				if ( iter==content.end() ) {
					break;
				}

				continue;
			}
			else {
				iter--;
			}
		}

		if ( *iter=='\n' ) {
			lineCount++;
		}

		text.push_back(*iter++);
	}

	if ( !text.empty() ) {
		script.append(makeResponse(text));
	}
	else if ( !directive.empty() ) 
	{
		std::string output;
		if ( executeDirective(directive,httpRequest,output) ) {
			script.append(output);
		}
		else {
			return false;
		}
	}

	return true;
}
std::tuple<std::unique_ptr<HTTPMessage>, std::unique_ptr<folly::IOBuf> >
makeResponse(uint16_t statusCode, size_t len) {
  auto resp = makeResponse(statusCode);
  resp->getHeaders().set(HTTP_HEADER_CONTENT_LENGTH, folly::to<string>(len));
  return std::make_pair(std::move(resp), makeBuf(len));
}
std::string
BaseCommand::makeResponse(const std::string &type, const std::string &status)
{
    Yb::ElementTree::ElementPtr empty_body;
    return makeResponse(type, status, empty_body);
}