/*
 * Sends a formatted response message to Sock with the given
 * status code and content type. The value of ContentType can
 * be NULL if no ContentType is required.
 */
static void httpSendStatusLine(Ssock_Handle ssock, int status, const char * contentType)
{
    char buf[MAXRESPONSESIZE];
    const char * msg;
    int len;

    Log_print1(Diags_ANALYSIS, "sendStatus> %d", (IArg)status);

    if (status < 0 || status > 999) {
        status = 999;
    }

    msg = getStatusMessage(status);

    if (contentType) {
        len = esnprintf(buf, sizeof(buf), "%s %3d %s\r\n%s%s\r\n", HTTP_VER,
                        status, msg, CONTENT_TYPE, contentType);
    }
    else {
        len = esnprintf(buf, sizeof(buf), "%s %3d %s\r\n\r\n", HTTP_VER,
                        status, msg);
    }

    if (len == sizeof(buf)) {
        Log_print1(Diags_WARNING, "sendStatus> Buffer size too small: %d", sizeof(buf));
    }

    Ssock_send(ssock, buf, len, 0);
}
void HTTPSrv_sendResponse(Ssock_Handle ssock, int status, const char * type,
                        size_t len, const void * buf)
{
    httpSendStatusLine(ssock, status, type);
    httpSendEntityLength(ssock, len);
    if (len > 0 && buf) {
        Ssock_send(ssock, buf, len, 0);
    }
}
/*
 * Write out the entiry length tag, and terminates the header
 * with an additional CRLF.
 *
 * Since the header is terminated, this must be the last tag
 * written. Entity data should follow immediately.
 */
static void httpSendEntityLength(Ssock_Handle ssock, int32_t entityLen)
{
    char buf[32]; /* sizeof("Content-length: ") + 10 + 4 + 1]; */
    int len;

    len = esnprintf(buf, sizeof(buf), "%s%d\r\n\r\n", CONTENT_LENGTH, entityLen);
    if (len == sizeof(buf)) {
        Log_print1(Diags_WARNING, "sendEntityLength> Buffer size too small: %d",
                   sizeof(buf));
    }

    Ssock_send(ssock, buf, len, 0);
}
Exemple #4
0
int iot_tls_write(Network *pNetwork, unsigned char *pMsg, int len,
            int timeout_ms)
{
    Ssock_Handle ssock = NULL;
    int bytes = 0;

    if (pNetwork == NULL || pMsg == NULL || pNetwork->my_socket == 0
            || timeout_ms == 0) {
        return (NULL_VALUE_ERROR);
    }

    ssock = (Ssock_Handle)pNetwork->my_socket;

    bytes = Ssock_send(ssock, pMsg, len, 0);
    if (bytes >= 0) {
       return (bytes);
    }

    return (SSL_WRITE_ERROR);
}