Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 8 of 8 total
Thread Unwanted HTTP header
Fri, Mar 2 2018 2:59 AMPermanent Link

Stephen P

Prokon Software Consultants (Canada) Ltd.

My first post here Smile

I am building a EWB server module. When setting the Content-Type header for the server response, I am seeing an unwanted "Content-Type:text/html; charset=utf-8" header in all responses. Below is an example where I use SendContent() to return plain text, and where I use SendContentStream() to return a JPEG image. In both cases the Content-Type header that I specify is present in the response, but there is also a unwanted second Content-Type header that specifies HTML, which then messes up what should be rendered in the web browser.

For the sample code below, http://localhost/modules/requesttestmodule?action=test-sendcontent should return text/plain text. However, the response headers show:
Content-Type:text/plain
Content-Type:text/html; charset=utf-8

For http://localhost/modules/requesttestmodule?action=test-sendcontentstream, I am expecting image/jpg, but the response headers show:
Content-Type:image/jpg
Content-Type:text/html; charset=utf-8

I am unable to get rid of the second Content-Type header. What am I missing?

Thank you for your response!


Sample code for a basic EWB module
------------------------------------------------

unit unit1;

interface

uses SysUtils, Classes, DB, ewbhttpmodule, ewbhttpcommon;

type
 TEWBModule1 = class(TEWBModule)
   procedure EWBModuleExecute(Request: TEWBServerRequest);
 private
   { Private declarations }
 public
   { Public declarations }
 end;

var
 EWBModule1: TEWBModule1;

implementation

{$R *.dfm}

procedure TEWBModule1.EWBModuleExecute(Request: TEWBServerRequest);
var
 MemoryStream: TMemoryStream;
begin
 case Request.RequestMethod of
   rmGet:
     begin
       if AnsiSameText(Request.RequestParams.Values['action'], 'test-sendcontent') then
       begin
         Request.ResponseHeaders.Clear;  // Clear any default headers
         Request.ResponseHeaders.Add('Cache-Control: no-cache');
         Request.ResponseHeaders.Add('Content-Type: text/plain');  // Set plain text
         Request.SendContent('Plean text response to test-sendcontent');
       end
       else if AnsiSameText(Request.RequestParams.Values['action'], 'test-sendcontentstream') then
       begin
         MemoryStream := TMemoryStream.Create;
         MemoryStream.LoadFromFile('C:\EWB\SomeImage.jpg');  // Specify a JPEG image somewhere on the server
         Request.ResponseHeaders.Clear;    // Clear any default headers
         Request.ResponseHeaders.Add('Cache-Control: no-cache');
         Request.ResponseHeaders.Add('Content-Type: image/jpg');
         Request.SendContentStream(MemoryStream);
     end;
 end;
end;

end.



Attachments: RequestModule.zip
Fri, Mar 2 2018 10:06 AMPermanent Link

Walter Matte

Tactical Business Corporation

I don't use the EWB Webserver as I wrote my own RealThinClient webserver... so I may be way off base here - but I saw one post in the forum using SendCustomContent:


     SendCustomContent(UTF8Encode(BuildClientInfo), 'application/json; charset=utf-8', '');

Maybe you could return your response with this method?  

Hopefully someone who "knows" will jump in here....

Walter
Fri, Mar 2 2018 10:11 AMPermanent Link

Walter Matte

Tactical Business Corporation

See Documentation

https://www.elevatesoft.com/manual?action=viewtopic&id=ewb2mod&product=rsdelphiwin32&version=10S&topic=Handling_Requests

Scroll Down


SendCustomContent

SendCustomContentStream

Walter
Fri, Mar 2 2018 1:48 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Stephen,

<< My first post here Smile>>

Welcome !

<< I am building a EWB server module. When setting the Content-Type header for the server response, I am seeing an unwanted "Content-Type:text/html; charset=utf-8" header in all responses. Below is an example where I use SendContent() to return plain text, and where I use SendContentStream() to return a JPEG image. In both cases the Content-Type header that I specify is present in the response, but there is also a unwanted second Content-Type header that specifies HTML, which then messes up what should be rendered in the web browser. >>

As Walter indicated, what you want is this:

https://www.elevatesoft.com/manual?action=viewmethod&id=ewb2mod&product=rsdelphiwin32&version=10T&comp=TEWBServerRequest&method=SendCustomContent

or this:

https://www.elevatesoft.com/manual?action=viewmethod&id=ewb2mod&product=rsdelphiwin32&version=10T&comp=TEWBServerRequest&method=SendCustomContentStream

Tim Young
Elevate Software
www.elevatesoft.com
Sat, Mar 3 2018 10:18 AMPermanent Link

Stephen P

Prokon Software Consultants (Canada) Ltd.

Thanks for the friendly welcome and no fewer than three replies in 24 hours!

@Walter: I understand where you are coming from with your server suggestion. We will probably be using Apache and WebBroker modules on some other projects. For this current project, however, I value all the automatic database magic that EWB Server provides. There is some functionality that will be unusual, and for that reason I am building a EWB Server module.

@Walter: I did not consider using SendCustomContent or SendCustomContentStream because the requirement for a Content-Disposition suggests that one is downloading a file rather than rendering content. I tried it and found the following:
1) The Content-Type header is not duplicated with a "charset=utf-8" anymore, hurrah!
2) However, some web browsers (e.g. Chrome) insist on saving (downloading) the server response to file rather than just displaying it. Others (e.g. Edge) are happy to just display the response. So, using SendCustomContent or SendCustomContentStream will work well when you want to download a file, but DOES NOT SOLVE my pain where I just want to display the response in the browser.

@Tim: My perception of the HTTP header behaviour that I described in the first post is that it is a bug. The documentation for ResponseHeaders.Add states that one can add (among others) a Content-Type header for the SendContent method. In my humble opinion, it is incorrect for the SendContent method to add its own (second) "Content-Type: charset=utf-8" header. And by the same token (even if not explicitly so mentioned in the documentation), the same applies to the SendContentStream method.

As things stand now, I am completely unable to use a Content-Type header of my choice and not have the web browser want to save (download) the response to a file.
Mon, Mar 5 2018 4:17 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Stephen,

<< However, some web browsers (e.g. Chrome) insist on saving (downloading) the server response to file rather than just displaying it. Others (e.g. Edge) are happy to just display the response. So, using SendCustomContent or SendCustomContentStream will work well when you want to download a file, but DOES NOT SOLVE my pain where I just want to display the response in the browser. >>

With what type of content are you seeing this behavior ?  If you *don't* specify a content disposition, then the browser *should* preference handling the content per the default behavior for the given content type (EWB doesn't send over a content disposition).  In other words, the default preference is to display a PDF instead of downloading it, and the same with image files.

<< My perception of the HTTP header behaviour that I described in the first post is that it is a bug. The documentation for ResponseHeaders.Add states that one can add (among others) a Content-Type header for the SendContent method. >>

I'm not sure what you're referring to, but this is the documentation for the ResponseHeaders property:

https://www.elevatesoft.com/manual?action=viewprop&id=ewb2mod&product=rsdelphiwin32&version=10T&comp=TEWBServerRequest&prop=ResponseHeaders

It states which headers are and are not sent with the various methods.  It *could* use a little more information on how the headers are set by the methods, and I'll fix that.

Tim Young
Elevate Software
www.elevatesoft.com
Wed, Mar 7 2018 10:10 AMPermanent Link

Stephen P

Prokon Software Consultants (Canada) Ltd.

<< With what type of content are you seeing this behavior ?  If you *don't* specify a content disposition, then the browser *should* preference handling the content per the default behavior for the given content type (EWB doesn't send over a content disposition).  In other words, the default preference is to display a PDF instead of downloading it, and the same with image files. >>
The SendCustomContent and SendCustomContentStream methods have a required third parameter for Content-Disposition. When I specify a non-empty string for Content-Disposition, then I get the downloading behaviour in the web browsers. However, I just tried an empty string (this did not occur to me before) and now the behaviour is what I had hoped for. In other words, use SendCustomContent and SendCustomContentStream with Content-Type header of my choice and EMPTY value for Content-Disposition works 100%,

<< I'm not sure what you're referring to, but this is the documentation for the ResponseHeaders property >>
I am referring to following page in the manual:
https://www.elevatesoft.com/manual?action=viewprop&id=ewb2mod&product=rsdelphiwin32&version=10T&comp=TEWBServerRequest&prop=ResponseHeaders

The page indicates that one can set a Content-Type header to use with the SendContent. My experience is that SendContent honours my instructions to set that header, but then always adds in its own "Content-Type:text/html; charset=utf-8" in addition to my header. There can only be one Content-Type header. So, IMHO this is a bug -- SendContent should use my header (if specified) and not its own default header.

By the same token (while not mentioned in the manual), I feel that the same should be applicable to SendContentStream.

I hope you agree! Thanks for considering my feedback,

Stephen
Fri, Mar 9 2018 2:05 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Stephen,

<< The page indicates that one can set a Content-Type header to use with the SendContent. >>

Actually, that's the *opposite* of what it is trying to say, so I'll make sure to clarify the topic so that it's more clear.

<< My experience is that SendContent honours my instructions to set that header, but then always adds in its own "Content-Type:text/html; charset=utf-8" in addition to my header. There can only be one Content-Type header. So, IMHO this is a bug -- SendContent should use my header (if specified) and not its own default header. >>

I'll see what I can do, but for now you're going to have to use SendCustomContent to do what you want.

Tim Young
Elevate Software
www.elevatesoft.com
Image