MQX 4.1 HTTPSRV: How to modify the reponse header?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MQX 4.1 HTTPSRV: How to modify the reponse header?

892 Views
pbanta
Contributor IV

I'm working on updating a project from MQX 3.8.1 to MQX 4.1.  The most major change is the new HTTPSRV API and the associated changes.  With the old HTTPD server I was able to modify the response header like so:

/* Send the reply */

session->response.contenttype = CONTENT_TYPE_HTML;

session->response.statuscode = 302;

strncat(session->response.header, "Location: control.html\n", HTTPDCFG_MAX_HEADER_LEN);

httpd_sendhdr(session, 0, 0);

How can do the same with MQX 4.1?  session->response no longer has a header field.

Thanks,

Paul

Labels (1)
0 Kudos
2 Replies

571 Views
karelm_
Contributor IV

Hi Paul,

it is no longer possible to modify a header of HTTP response directly. However in your case you can use a redirection in HTML using the http-equip attribute of the <meta> tag (see HTML meta http-equiv Attribute). And compose response using this tag in your CGI. Here is an example:

response.content_type = HTTPSRV_CONTENT_TYPE_HTML;

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

"<html><head><title>My Title</title>"

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

response.data_length = strlen(response.data);

response.content_length = 0;

HTTPSRV_cgi_write(&response);

Edit:

Regarding the content length: If it is set to zero, HTTP server will signalize end of response entity by closing the connection and thus ignoring keep-alive flag of session.

Edit2:

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 Kudos

571 Views
pbanta
Contributor IV

Hi Karel,

Thanks for the reply.  I'll give the http-equip attribute a try.  Looks like a good alternative as long as it works with IE, Firefox, Chrome, Safari.

Best regards,

Paul

0 Kudos