示例#1
0
    /**
     *
     * @param uri
     * @param data
     */
    void Rest::patch(const std::string& uri, Json::Value data)
    {
        Config& config = Config::getInstance();

        Json::FastWriter jwriter;

        auto conn = std::make_shared<RestClient::Connection>(config.api_host);

        conn->AppendHeader("X-Auth-Token", m_api_token);
        conn->AppendHeader("Content-Type", "application/json");

        RestClient::Response response = conn->patch(uri, jwriter.write(data));

        if (response.code == 200) {
            std::cout << "API. Resource Updated" << std::endl;
        } else if (response.code == 201) {
            std::cout << "API. Resource Created" << std::endl;
        } else {
            if (response.code == 401) {
                get_token();
            }

            std::cerr << "RestClient HTTP response code: " << response.code << std::endl;
            std::cerr << "URL: " << config.api_host << uri << std::endl;

            throw Rest::RestapiException("RestClient error");
        }
    }
示例#2
0
    /**
     * Query API. Return json value
     *
     * @param uri
     * @return JSON value
     */
    Json::Value Rest::get(const std::string& uri)
    {
        Config& config = Config::getInstance();

        auto conn = std::make_shared<RestClient::Connection>(config.api_host);

        conn->AppendHeader("X-Auth-Token", m_api_token);
        conn->AppendHeader("Content-Type", "application/json");
        RestClient::Response response = conn->get(uri);

        if (response.code != 200) {
            if (response.code == 401) {
                get_token();
            }

            m_errors_count++;

            std::cerr << "RestClient HTTP response code: " << response.code << std::endl;
            std::cerr << "URL: " << config.api_host << uri << std::endl;

            throw Rest::RestapiException("RestClient error");
        } else {
            m_errors_count = 0;

            Json::Value jvalue;
            Json::Reader jreader(Json::Features::strictMode());

            if (jreader.parse(response.body, jvalue, false)) {
                return jvalue;
            } else {
                throw Rest::RestapiException("JSON Parse error");
            }
        }
    }
void RTSPRequestInterface::WriteStandardHeaders()
{
    static StrPtrLen    sCloseString("Close", 5);

    Assert(sPremadeHeader != NULL);
    fStandardHeadersWritten = true; //must be done here to prevent recursive calls
    
    //if this is a "200 OK" response (most HTTP responses), we have some special
    //optmizations here
    Bool16 sendServerInfo = QTSServerInterface::GetServer()->GetPrefs()->GetRTSPServerInfoEnabled();
    if (fStatus == qtssSuccessOK)
    {
        
        if (sendServerInfo)
        {   fOutputStream->Put(sPremadeHeaderPtr);
        }
        else
        {
            fOutputStream->Put(sPremadeNoHeaderPtr);
        }
        StrPtrLen* cSeq = fHeaderDictionary.GetValue(qtssCSeqHeader);
        Assert(cSeq != NULL);
        if (cSeq->Len > 1)
            fOutputStream->Put(*cSeq);
        else if (cSeq->Len == 1)
            fOutputStream->PutChar(*cSeq->Ptr);
        fOutputStream->PutEOL();
    }
    else
    {
#if 0
		// if you want the connection to stay alive when we don't grok
		// the specfied parameter than eneable this code. - [sfu]
		if (fStatus == qtssClientParameterNotUnderstood) {
			fResponseKeepAlive = true;
		}
#endif 
        //other status codes just get built on the fly
        PutStatusLine(fOutputStream, fStatus, RTSPProtocol::k10Version);
        if (sendServerInfo)
        {
            fOutputStream->Put(QTSServerInterface::GetServerHeader());
            fOutputStream->PutEOL();
        }
        AppendHeader(qtssCSeqHeader, fHeaderDictionary.GetValue(qtssCSeqHeader));
    }

    //append sessionID header
    StrPtrLen* incomingID = fHeaderDictionary.GetValue(qtssSessionHeader);
    if ((incomingID != NULL) && (incomingID->Len > 0))
        AppendHeader(qtssSessionHeader, incomingID);

    //follows the HTTP/1.1 convention: if server wants to close the connection, it
    //tags the response with the Connection: close header
    if (!fResponseKeepAlive)
        AppendHeader(qtssConnectionHeader, &sCloseString);
}
示例#4
0
int
AppendHeaderWho(uint8_t* aRetBuf, int aBufferSize, const uint8_t* aWho,
                int aLength)
{
  return AppendHeader(ObexHeaderId::Who, aRetBuf, aBufferSize,
                      aWho, aLength);
}
示例#5
0
otError Dhcp6Server::SendReply(otIp6Address &aDst, uint8_t *aTransactionId, ClientIdentifier &aClient, IaNa &aIaNa)
{
    otError error = OT_ERROR_NONE;
    Ip6::MessageInfo messageInfo;
    Message *message;

    VerifyOrExit((message = mSocket.NewMessage(0)) != NULL, error = OT_ERROR_NO_BUFS);
    SuccessOrExit(error = AppendHeader(*message, aTransactionId));
    SuccessOrExit(error = AppendServerIdentifier(*message));
    SuccessOrExit(error = AppendClientIdentifier(*message, aClient));
    SuccessOrExit(error = AppendIaNa(*message, aIaNa));
    SuccessOrExit(error = AppendStatusCode(*message, kStatusSuccess));
    SuccessOrExit(error = AppendIaAddress(*message, aClient));
    SuccessOrExit(error = AppendRapidCommit(*message));

    memset(&messageInfo, 0, sizeof(messageInfo));
    memcpy(&messageInfo.GetPeerAddr().mFields.m8, &aDst, sizeof(otIp6Address));
    messageInfo.mPeerPort = kDhcpClientPort;
    SuccessOrExit(error = mSocket.SendTo(*message, messageInfo));

exit:

    if (message != NULL && error != OT_ERROR_NONE)
    {
        message->Free();
    }

    return error;
}
示例#6
0
int
AppendHeaderAppParameters(uint8_t* aRetBuf, int aBufferSize,
                          const uint8_t* aAppParameters, int aLength)
{
  return AppendHeader(ObexHeaderId::AppParameters, aRetBuf, aBufferSize,
                      aAppParameters, aLength);
}
示例#7
0
int
AppendHeaderBody(uint8_t* aRetBuf, int aBufferSize, const uint8_t* aBody,
                 int aLength)
{
  return AppendHeader(ObexHeaderId::Body, aRetBuf, aBufferSize,
                      aBody, aLength);
}
示例#8
0
int
AppendHeaderName(uint8_t* aRetBuf, int aBufferSize, const uint8_t* aName,
                 int aLength)
{
  return AppendHeader(ObexHeaderId::Name, aRetBuf, aBufferSize,
                      aName, aLength);
}
示例#9
0
    /**
     *
     * @param uri
     * @param data
     */
    void Rest::post(const std::string& uri, Json::Value data)
    {
        Config& config = Config::getInstance();

        Json::FastWriter jwriter;

        auto conn = std::make_shared<RestClient::Connection>(config.api_host);

        conn->AppendHeader("X-Auth-Token", m_api_token);
        conn->AppendHeader("Content-Type", "application/json");

        std::string dt = jwriter.write(data);

        RestClient::Response response = conn->post(uri, jwriter.write(data));

        if (response.code == 201) {
            m_errors_count = 0;
            std::cout << "API. Resource Created" << std::endl;
        } else if (response.code == 422) {
            std::cerr << "RestClient HTTP response code: " << response.code << std::endl;
            std::cerr << "URL: " << config.api_host << uri << std::endl;

            Json::Value jvalue;
            Json::Reader jreader(Json::Features::strictMode());

            if (jreader.parse(response.body, jvalue, false)) {
                std::cerr << "Error: " << jvalue["message"].asString() << std::endl;
            } else {
                std::cerr << "Error: " << response.body << std::endl;
            }

        } else {
            if (response.code == 401) {
                get_token();
            }

            m_errors_count++;

            std::cerr << "RestClient HTTP response code: " << response.code << std::endl;
            std::cerr << "URL: " << config.api_host << uri << std::endl;

            throw Rest::RestapiException("RestClient error");
        }
    }
void SecOTRAppendSignatureMessage(SecOTRSessionRef session,
                                        CFMutableDataRef appendTo)
{
    //
    // Message Type: kSignatureMessage (0x12)
    // G^X Data MPI
    //
    
    AppendHeader(appendTo, kSignatureMessage);
    AppendMACedEncryptedSignature(session, true, appendTo);
}
void SecOTRAppendDHKeyMessage(SecOTRSessionRef session,
                              CFMutableDataRef appendTo)
{
    //
    // Message Type: kDHKeyMessage (0x0A)
    // G^X Data MPI
    //
    
    AppendHeader(appendTo, kDHKeyMessage);
    SecFDHKAppendPublicSerialization(session->_myKey, appendTo);
}
示例#12
0
    /**
     * Get Token. Set Token to variable api_token
     *
     * @return int
     */
    int Rest::get_token()
    {
        Config& config = Config::getInstance();

        auto conn = std::make_shared<RestClient::Connection>(config.api_host);

        std::string uri = "/gdaemon_api/get_token";
        conn->AppendHeader("Authorization", "Bearer " + config.api_key);
        conn->AppendHeader("Content-Type", "application/json");
        RestClient::Response response = conn->get(uri);

        if (response.code != 200) {
            std::cerr << "RestClient HTTP response code: " << response.code << std::endl;
            std::cerr << "URL: " << config.api_host << uri << std::endl;

            m_errors_count++;

            throw Rest::RestapiException("RestClient error. Token getting error.");
        } else {
            m_errors_count = 0;

            Json::Value jvalue;
            Json::Reader jreader(Json::Features::strictMode());

            if (jreader.parse(response.body, jvalue, false)) {
                if (!jvalue["token"].isString()) {
                    throw Rest::RestapiException("Invalid token");
                }

                Rest::m_api_token = jvalue["token"].asString();

            } else {
                throw Rest::RestapiException("JSON Parse error");
            }
        }

        return 0;
    }
void SecOTRAppendRevealSignatureMessage(SecOTRSessionRef session,
                                        CFMutableDataRef appendTo)
{
    //
    // Message Type: kRevealSignatureMessage (0x11)
    // G^X Data MPI
    //
    
    AppendHeader(appendTo, kRevealSignatureMessage);
    
    AppendLong(appendTo, kOTRAuthKeyBytes);
    uint8_t* keyPosition = CFDataIncreaseLengthAndGetMutableBytes(appendTo, kOTRAuthKeyBytes);
    memcpy(keyPosition, session->_r, kOTRAuthKeyBytes);
    
    AppendMACedEncryptedSignature(session, false, appendTo);
}
void SecOTRAppendDHMessage(SecOTRSessionRef session,
                           CFMutableDataRef appendTo)
{
    //
    // Message Type: kDHMessage (0x02)
    // AES_CTR(r, 0) of G^X MPI
    // SHA256(gxmpi)
    //
    
    if(!session) return;
    CFMutableDataRef gxmpi = CFDataCreateMutable(kCFAllocatorDefault, 0);
    if(!gxmpi) return;

    AppendHeader(appendTo, kDHMessage);

    SecFDHKAppendPublicSerialization(session->_myKey, gxmpi);

    size_t gxmpiSize = (size_t)CFDataGetLength(gxmpi);
    if(gxmpiSize == 0) {
        CFReleaseNull(gxmpi);
        return;
    }
    const uint8_t* gxmpiLocation = CFDataGetBytePtr(gxmpi);

    /* 64 bits cast: gxmpiSize is the size of the EC public key, which is hardcoded and never more than 2^32 bytes. */
    assert(gxmpiSize<UINT32_MAX); /* debug check only */
    AppendLong(appendTo, (uint32_t)gxmpiSize);
    assert(gxmpiSize<INT32_MAX);
    uint8_t* encGxmpiLocation = CFDataIncreaseLengthAndGetMutableBytes(appendTo, (CFIndex)gxmpiSize);
    AES_CTR_IV0_Transform(sizeof(session->_r), session->_r, gxmpiSize, gxmpiLocation, encGxmpiLocation);
    
    AppendLong(appendTo, CCSHA256_OUTPUT_SIZE);
    uint8_t* hashLocation = CFDataIncreaseLengthAndGetMutableBytes(appendTo, CCSHA256_OUTPUT_SIZE);

#ifdef USECOMMONCRYPTO
    (void) CC_SHA256(gxmpiLocation, (uint32_t)gxmpiSize, hashLocation);
#else
    ccdigest(ccsha256_di(), gxmpiSize, gxmpiLocation, hashLocation);
#endif
    CFReleaseNull(gxmpi);
}
示例#15
0
void
HttpResponse::ParseHeaders (const char *input)
{
    const char *ptr;
    const char *start = input;

    VERIFY_MAIN_THREAD;
    LOG_DOWNLOADER ("HttpResponse::ParseHeaders:\n%s\n\n", input);

    if (start == NULL || start [0] == 0)
        return;

    /*
     * Parse status line
     */

    /* Find http version */
    ptr = start;
    while (*ptr != 0 && *ptr != '\n' && *ptr != '\r') {
        if (*ptr == ' ') {
            LOG_DOWNLOADER ("ParseHeaders: header version: %.*s\n", (int) (ptr - start), start);
            break;
        }
        ptr++;
    };
    /* Skip extra spaces */
    while (*ptr == ' ')
        ptr++;
    if (*ptr == '\n' || *ptr == '\r') {
        LOG_DOWNLOADER ("ParseHeaders: invalid status line\n");
        return;
    }

    /* Find status code */
    start = ptr;
    while (*ptr != 0 && *ptr != '\n' && *ptr != '\r') {
        if (*ptr == ' ') {
            response_status = atoi (start);
            LOG_DOWNLOADER ("ParseHeaders: status code: %i\n", response_status);
            break;
        }
        ptr++;
    }
    /* Skip extra spaces */
    while (*ptr == ' ')
        ptr++;
    if (*ptr == '\n' || *ptr == '\r') {
        LOG_DOWNLOADER ("ParseHeaders: invalid status line\n");
        return;
    }
    /* The rest is status text */
    start = ptr;
    while (*ptr != 0) {
        if (*ptr == '\n' || *ptr == '\r') {
            response_status_text = g_strndup (start, ptr - start);
            LOG_DOWNLOADER ("ParseHeaders: status text: %s\n", response_status_text);
            break;
        }
        ptr++;
    }

    start = ptr;


    /*
     * Parse header lines
     */
    char *header = NULL;
    char *value = NULL;

    while (*ptr != 0) {
        if (header == NULL && *ptr == ':') {
            header = g_strndup (start, ptr - start);
            while (*(ptr + 1) == ' ')
                ptr++;
            start = ptr + 1;
        } else if (*ptr == '\n' || *ptr == '\r') {
            if (header != NULL) {
                value = g_strndup (start, ptr - start);
                AppendHeader (header, value);
            }
            start = ptr + 1;
            g_free (header);
            header = NULL;
            g_free (value);
            value = NULL;
        }
        ptr++;
    }

    if (value != NULL && header != NULL)
        AppendHeader (header, value);
    g_free (header);
    g_free (value);
}
示例#16
0
int
AppendHeaderConnectionId(uint8_t* aRetBuf, int aConnectionId)
{
  return AppendHeader(ObexHeaderId::ConnectionId, aRetBuf, aConnectionId);
}
示例#17
0
int
AppendHeaderLength(uint8_t* aRetBuf, int aObjectLength)
{
  return AppendHeader(ObexHeaderId::Length, aRetBuf, aObjectLength);
}