HTTPサーバで複数のレスポンスで応答するにはどうすればよいですか?

弊社TCP/IPプロトコルスタック「μNet3」に関するご質問
返信する
アバター
eForce技術担当
記事: 193
登録日時: 2014年4月24日(木) 14:18

HTTPサーバで複数のレスポンスで応答するにはどうすればよいですか?

投稿記事 by eForce技術担当 » 2014年7月14日(月) 15:10

たとえば動的にコンテンツサイズが決定するケースなど、Content-Length:が未定の場合
以下のようにCGI関数にて複数回のレスポンスでコンテンツを返却します。

コード: 全て選択

/* 100回のレスポンスでコンテンツを返却する例 */

#define REPEAT 100
void cgi_func(T_HTTP_SERVER *http)
{
    int i;
    char contents[] = "Divide Contents Test <p>";

    HttpSetContent(http, 0);
    HttpSetContent(http, "HTTP/1.1 200 OK\r\n");
    HttpSetContent(http, "Content-Type: text/html\r\n");
    HttpSetContentLen(http, "Content-Length: ", (sizeof(contents)-1)*REPEAT);
    HttpSetContent(http, "\r\n");

    HttpSendContent(http, NULL, 0);       /* レスポンスヘッダのみ送信 */

    i = 0;
    while (i < REPEAT) {
        HttpSetContent(http, 0);
        HttpSetContent(http, contents);   /* 部分的なコンテンツを送信 */
        HttpSendContent(http, NULL, 0);
        i++;
    }
}

返信する