MQX 4.1 RTCS web_hvac response.content_length question

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

MQX 4.1 RTCS web_hvac response.content_length question

663件の閲覧回数
pbanta
Contributor IV

It's not clear to me how to set response.content_length in my CGI functions.

The MQX_RTCS User Guide says in section 8.2.14:

content_length

Length of the response entity from CGI script.

If you look at the web_hvac example there are conflicting examples of how to set the content_length field.

Example 1 - content_length is set to data_length

In cgi_index.c there is this code:

/* Calculate content length while saving it to buffer */

length = snprintf(str, BUFF_SIZE, "%ld\n%ld\n%ld\n", hour, min, sec);

response.data = str;

response.data_length = length;

response.content_length = response.data_length;

/* Send response */

HTTPSRV_cgi_write(&response);

return (response.content_length);

This seems to match what it says in the MQX RTCS User Guide.

Example 2 - content_length is set to zero

In cgi_hvac.c there is this code:

response.ses_handle = param->ses_handle;

response.content_type = HTTPSRV_CONTENT_TYPE_HTML;

response.status_code = 200;

response.data = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"

    "<html><head><title>HVAC Settings response</title>"

    "<meta http-equiv=\"REFRESH\" content=\"0;url=hvac.shtml\"></head>\n<body>\n";

response.data_length = strlen(response.data);

response.content_length = 0;

HTTPSRV_cgi_write(&response);

if (!bParams)

{

    response.data = "No parameters received.<br>\n";

    response.data_length = strlen(response.data);

    HTTPSRV_cgi_write(&response);

}

response.data = "<br><br>\n</body></html>";

response.data_length = strlen(response.data);

HTTPSRV_cgi_write(&response);

return (response.content_length);

In this case there are multiple calls to HTTPSRV_cgi_write() and content_length is set to zero on line 8.  It is never set to non-zero.

My question:

1. When should content_length be set to data_length and when should it be set to zero?

Thanks,

Paul

ラベル(1)
0 件の賞賛
返信
1 返信

470件の閲覧回数
karelm_
Contributor IV

Hi Paul,

If content_length is set to zero in first call of HTTPSRV_cgi_write(), The server will signalize end of response entity by closing the connection and thus ignoring keep-alive flag of session. If you set content_length to non-zero value it is send to client to inform him about length of response entity and thus it is not required to close the connection and keep-alive can be used. Variable data_length is used for storing length of data to be written in one specific call of HTTPSRV_cgi_write().


So in short:

  • You MUST set content_length non-zero if you have keep-alive enabled. Otherwise client will not know that it has all the data and the connection will stall. But you CAN have it set to zero with keep-alive disabled.
  • Set data_length to length of data pointed to by data variable for every call of HTTPSRV_cgi_write().


I hope this answered your question.


P.S: In MQX 4.2 there will be support for chunked transmission encoding so you will not have to worry about content length.

Best regards,

Karel

0 件の賞賛
返信