static HTTPAPI_RESULT OpenXIOConnection()

in adapters/httpapi_compact.c [730:817]


static HTTPAPI_RESULT OpenXIOConnection(HTTP_HANDLE_DATA* http_instance)
{
    HTTPAPI_RESULT result;

    if (http_instance->is_connected != 0)
    {
        /*Codes_SRS_HTTPAPI_COMPACT_21_033: [ If the whole process succeed, the HTTPAPI_ExecuteRequest shall retur HTTPAPI_OK. ]*/
        result = HTTPAPI_OK;
    }
    else
    {
        http_instance->is_io_error = 0;

        /*Codes_SRS_HTTPAPI_COMPACT_21_022: [ If a Certificate was provided, the HTTPAPI_ExecuteRequest shall set this option on the transport layer. ]*/
        if ((http_instance->certificate != NULL) &&
            (xio_setoption(http_instance->xio_handle, OPTION_TRUSTED_CERT, http_instance->certificate) != 0))
        {
            /*Codes_SRS_HTTPAPI_COMPACT_21_023: [ If the transport failed setting the Certificate, the HTTPAPI_ExecuteRequest shall not send any request and return HTTPAPI_SET_OPTION_FAILED. ]*/
            result = HTTPAPI_SET_OPTION_FAILED;
            LogInfo("Could not load certificate");
        }
        /*Codes_SRS_HTTPAPI_COMPACT_06_003: [ If the x509 client certificate is provided, the HTTPAPI_ExecuteRequest shall set this option on the transport layer. ]*/
        else if ((http_instance->x509ClientCertificate != NULL) &&
            (xio_setoption(http_instance->xio_handle, SU_OPTION_X509_CERT, http_instance->x509ClientCertificate) != 0))
        {
            /*Codes_SRS_HTTPAPI_COMPACT_06_005: [ If the transport failed setting the client certificate, the HTTPAPI_ExecuteRequest shall not send any request and return HTTPAPI_SET_OPTION_FAILED. ]*/
            result = HTTPAPI_SET_OPTION_FAILED;
            LogInfo("Could not load the client certificate");
        }
        else if ((http_instance->x509ClientPrivateKey != NULL) &&
            (xio_setoption(http_instance->xio_handle, SU_OPTION_X509_PRIVATE_KEY, http_instance->x509ClientPrivateKey) != 0))
        {

            /*Codes_SRS_HTTPAPI_COMPACT_06_006: [ If the transport failed setting the client certificate private key, the HTTPAPI_ExecuteRequest shall not send any request and return HTTPAPI_SET_OPTION_FAILED. ] */
            result = HTTPAPI_SET_OPTION_FAILED;
            LogInfo("Could not load the client certificate private key");
        }
        else if ((http_instance->tls_renegotiation == true) &&
            (xio_setoption(http_instance->xio_handle, OPTION_SET_TLS_RENEGOTIATION, &http_instance->tls_renegotiation) != 0))
        {
            result = HTTPAPI_SET_OPTION_FAILED;
            LogInfo("Could not load renegotiation flag");
        }
        else
        {
            /*Codes_SRS_HTTPAPI_COMPACT_21_024: [ The HTTPAPI_ExecuteRequest shall open the transport connection with the host to send the request. ]*/
            if (xio_open(http_instance->xio_handle, on_io_open_complete, http_instance, on_bytes_received, http_instance, on_io_error, http_instance) != 0)
            {
                /*Codes_SRS_HTTPAPI_COMPACT_21_025: [ If the open process failed, the HTTPAPI_ExecuteRequest shall not send any request and return HTTPAPI_OPEN_REQUEST_FAILED. ]*/
                result = HTTPAPI_OPEN_REQUEST_FAILED;
            }
            else
            {
                int countRetry;
                /*Codes_SRS_HTTPAPI_COMPACT_21_033: [ If the whole process succeed, the HTTPAPI_ExecuteRequest shall retur HTTPAPI_OK. ]*/
                result = HTTPAPI_OK;
                /*Codes_SRS_HTTPAPI_COMPACT_21_077: [ The HTTPAPI_ExecuteRequest shall wait, at least, 10 seconds for the SSL open process. ]*/
                countRetry = MAX_OPEN_RETRY;
                while ((http_instance->is_connected == 0) &&
                    (http_instance->is_io_error == 0))
                {
                    xio_dowork(http_instance->xio_handle);
                    LogInfo("Waiting for TLS connection");
                    if ((countRetry--) < 0)
                    {
                        /*Codes_SRS_HTTPAPI_COMPACT_21_078: [ If the HTTPAPI_ExecuteRequest cannot open the connection in 10 seconds, it shall fail and return HTTPAPI_OPEN_REQUEST_FAILED. ]*/
                        LogError("Open timeout. The HTTP request is incomplete");
                        result = HTTPAPI_OPEN_REQUEST_FAILED;
                        break;
                    }
                    else
                    {
                        /*Codes_SRS_HTTPAPI_COMPACT_21_083: [ The HTTPAPI_ExecuteRequest shall wait, at least, 100 milliseconds between retries. ]*/
                        ThreadAPI_Sleep(RETRY_INTERVAL_IN_MS);
                    }
                }
            }
        }
    }

    if ((http_instance->is_io_error != 0) && (result == HTTPAPI_OK))
    {
        /*Codes_SRS_HTTPAPI_COMPACT_21_025: [ If the open process failed, the HTTPAPI_ExecuteRequest shall not send any request and return HTTPAPI_OPEN_REQUEST_FAILED. ]*/
        result = HTTPAPI_OPEN_REQUEST_FAILED;
    }

    return result;
}