Skip to content

Commit e7e1bb5

Browse files
Add support for chunked httpclient and chuncked httpresponse.
1 parent ba3389e commit e7e1bb5

File tree

5 files changed

+98
-34
lines changed

5 files changed

+98
-34
lines changed

dotnet/src/dotnetframework/GxClasses/Core/Web/GxHttpServer.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ public class GxHttpResponse
9696
{
9797
HttpResponse _httpRes;
9898
IGxContext _context;
99-
99+
#if !NETCORE
100+
bool _chunkedResponse;
101+
#endif
100102
public GxHttpResponse(IGxContext context)
101103
{
102104
_context = context;
@@ -137,8 +139,14 @@ public void AddString( string s)
137139
{
138140
if (Response != null)
139141
{
142+
#if !NETCORE
143+
if (_chunkedResponse)
144+
{
145+
Response.Buffer = false;
146+
}
147+
#endif
140148
Response.Write(s);
141-
}
149+
}
142150
}
143151

144152
public void AddFile( string fileName)
@@ -150,14 +158,23 @@ public void AddFile( string fileName)
150158
}
151159
public void AppendHeader( string name, string value)
152160
{
153-
if(string.Compare(name, "Content-Disposition", true) == 0)
161+
bool transferEncodingHeader = (name.Equals(HttpHeader.TRANSFER_ENCODING, StringComparison.OrdinalIgnoreCase) && value.Equals(HttpHeaderValue.TRANSFER_ENCODING_CHUNKED, StringComparison.OrdinalIgnoreCase));
162+
#if !NETCORE
163+
if (transferEncodingHeader)
164+
_chunkedResponse = true;
165+
#endif
166+
167+
if (string.Compare(name, "Content-Disposition", true) == 0)
154168
{
155169
value = GXUtil.EncodeContentDispositionHeader(value, _context.GetBrowserType());
156170
}
157-
if (_context!=null)
158-
_context.SetHeader(name, value);
171+
if (!transferEncodingHeader)
172+
{
173+
if (_context != null)
174+
_context.SetHeader(name, value);
175+
}
159176
}
160-
177+
161178
}
162179

163180
public class GxSoapRequest : GxHttpRequest

dotnet/src/dotnetframework/GxClasses/Domain/GxHttpClient.cs

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public class GxHttpClient : IGxHttpClient, IDisposable
6666
const int StreamWriterDefaultBufferSize = 1024;
6767
Stream _sendStream;
6868
byte[] _receiveData;
69+
StreamReader _receiveStream;
6970
int _timeout = 30000;
7071
short _statusCode = 0;
7172
string _proxyHost;
@@ -414,6 +415,9 @@ public void AddHeader(string name, string value)
414415
GXLogging.Error(log, String.Format("Error parsing charset ", value, ex));
415416
}
416417
}
418+
if (name.Equals( HttpHeader.ACCEPT, StringComparison.OrdinalIgnoreCase) && value.Equals(HttpHeaderValue.ACCEPT_SERVER_SENT_EVENT, StringComparison.OrdinalIgnoreCase))
419+
_chunkedResponse = true;
420+
417421
_headers.Set(name, value);
418422
}
419423
public void ClearVariables()
@@ -675,6 +679,7 @@ HttpResponseMessage ExecuteRequest(string method, string requestUrl, CookieConta
675679
handler.ReceiveDataTimeout = milliseconds;
676680
handler.ReceiveHeadersTimeout = milliseconds;
677681
#endif
682+
678683
using (client = new HttpClient(handler))
679684
{
680685
client.Timeout = milliseconds;
@@ -697,7 +702,10 @@ HttpResponseMessage ExecuteRequest(string method, string requestUrl, CookieConta
697702
reqStream.Seek(0, SeekOrigin.Begin);
698703
request.Content = new ByteArrayContent(reqStream.ToArray());
699704
setHeaders(request, handler.CookieContainer);
700-
response = client.SendAsync(request).GetAwaiter().GetResult();
705+
if (_chunkedResponse)
706+
response = client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).GetAwaiter().GetResult();
707+
else
708+
response = client.SendAsync(request).GetAwaiter().GetResult();
701709
}
702710
}
703711
return response;
@@ -730,18 +738,27 @@ void ReadReponseContent(HttpResponseMessage response)
730738
charset = String.Empty;
731739
}
732740
}
733-
734-
using (MemoryStream ms = new MemoryStream())
741+
if (_chunkedResponse)
735742
{
736-
stream.CopyTo(ms);
737-
_receiveData = ms.ToArray();
743+
if (_encoding == null)
744+
_encoding = Encoding.UTF8;
745+
746+
_receiveStream = new StreamReader(stream, _encoding);
738747
}
739-
int bytesRead = _receiveData.Length;
740-
GXLogging.Debug(log, "BytesRead " + _receiveData.Length);
741-
if (bytesRead > 0 && !encodingFound)
748+
else
742749
{
743-
_encoding = DetectEncoding(charset, out encodingFound, _receiveData, bytesRead);
744-
}
750+
using (MemoryStream ms = new MemoryStream())
751+
{
752+
stream.CopyTo(ms);
753+
_receiveData = ms.ToArray();
754+
}
755+
int bytesRead = _receiveData.Length;
756+
GXLogging.Debug(log, "BytesRead " + _receiveData.Length);
757+
if (bytesRead > 0 && !encodingFound)
758+
{
759+
_encoding = DetectEncoding(charset, out encodingFound, _receiveData, bytesRead);
760+
}
761+
}
745762
}
746763
catch (IOException ioEx)
747764
{
@@ -787,7 +804,7 @@ public void Execute(string method, string name)
787804

788805
public void HttpClientExecute(string method, string name)
789806
{
790-
HttpResponseMessage response = null;
807+
response = null;
791808
Byte[] Buffer = new Byte[1024];
792809
_errCode = 0;
793810
_errDescription = string.Empty;
@@ -868,11 +885,15 @@ public void HttpClientExecute(string method, string name)
868885
_errDescription = "The remote server returned an error: (" + _statusCode + ") " + _statusDescription + ".";
869886
}
870887
ClearSendStream();
871-
GXLogging.DebugSanitized(log, "_responseString " + ToString());
888+
if (!_chunkedResponse)
889+
{
890+
GXLogging.DebugSanitized(log, "_responseString " + ToString());
891+
}
872892
}
873893
NameValueCollection _respHeaders;
874894
private bool disposedValue;
875-
895+
private bool _chunkedResponse;
896+
HttpResponseMessage response;
876897
void LoadResponseHeaders(HttpResponseMessage resp)
877898
{
878899
_respHeaders = new NameValueCollection();
@@ -1363,6 +1384,25 @@ private Encoding ExtractEncodingFromCharset(string responseText, string regExpP,
13631384

13641385
public override string ToString()
13651386
{
1387+
if (_chunkedResponse && _receiveStream != null)
1388+
{
1389+
if (!_receiveStream.EndOfStream)
1390+
{
1391+
string line= _receiveStream.ReadLine();
1392+
if (line != null)
1393+
return line;
1394+
else
1395+
return "EOF";
1396+
}
1397+
else
1398+
{
1399+
_receiveStream.Dispose();
1400+
_receiveStream = null;
1401+
response.Dispose();
1402+
response = null;
1403+
return "EOF";
1404+
}
1405+
}
13661406
if (_encoding == null)
13671407
_encoding = Encoding.UTF8;
13681408
if (_receiveData == null)

dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ public class HttpHeader
4848
public static string LAST_MODIFIED = "Last-Modified";
4949
public static string EXPIRES = "Expires";
5050
public static string XGXFILENAME = "x-gx-filename";
51+
internal static string ACCEPT = "Accept";
52+
internal static string TRANSFER_ENCODING = "Transfer-Encoding";
53+
}
54+
internal class HttpHeaderValue
55+
{
56+
internal static string ACCEPT_SERVER_SENT_EVENT = "text/event-stream";
57+
internal static string TRANSFER_ENCODING_CHUNKED = "chunked";
5158
}
5259
[DataContract()]
5360
public class HttpJsonError

dotnet/src/dotnetframework/GxClasses/Middleware/GXHttp.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,10 +1877,9 @@ public void ProcessRequest(HttpContext httpContext)
18771877
InitPrivates();
18781878
try
18791879
{
1880-
#if NETCORE
18811880
SendHeaders();
18821881
string clientid = context.ClientID; //Send clientid cookie (before response HasStarted) if necessary, since UseResponseBuffering is not in .netcore3.0
1883-
#endif
1882+
18841883
bool validSession = ValidWebSession();
18851884
if (validSession && IntegratedSecurityEnabled)
18861885
validSession = ValidSession();
@@ -1915,9 +1914,6 @@ public void ProcessRequest(HttpContext httpContext)
19151914
context.DispatchAjaxCommands();
19161915
}
19171916
SetCompression(httpContext);
1918-
#if !NETCORE
1919-
SendHeaders();
1920-
#endif
19211917
context.ResponseCommited = true;
19221918
}
19231919
catch (Exception e)

dotnet/src/dotnetframework/GxClasses/Model/GXWebProcedure.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class GXWebProcedure : GXHttpHandler
1818
protected IReportHandler oldReportHandler;
1919
string outputFileName;
2020
string outputType;
21+
bool fileContentInline;
2122

2223
protected int lineHeight;
2324
protected int Gx_line;
@@ -115,18 +116,20 @@ protected override void sendCacheHeaders()
115116

116117
private void setOuputFileName()
117118
{
118-
string fileName = GetType().Name;
119-
string fileType = "pdf";
120-
if (!string.IsNullOrEmpty(outputFileName))
119+
if (fileContentInline)
121120
{
122-
fileName = outputFileName;
123-
}
124-
if (!string.IsNullOrEmpty(outputType))
125-
{
126-
fileType = outputType.ToLower();
121+
string fileName = GetType().Name;
122+
string fileType = "pdf";
123+
if (!string.IsNullOrEmpty(outputFileName))
124+
{
125+
fileName = outputFileName;
126+
}
127+
if (!string.IsNullOrEmpty(outputType))
128+
{
129+
fileType = outputType.ToLower();
130+
}
131+
context.HttpContext.Response.AddHeader(HttpHeader.CONTENT_DISPOSITION, $"inline; filename={fileName}.{fileType}");
127132
}
128-
129-
context.HttpContext.Response.AddHeader(HttpHeader.CONTENT_DISPOSITION, $"inline; filename={fileName}.{fileType}");
130133
}
131134

132135
public virtual int getOutputType()
@@ -138,6 +141,7 @@ protected bool initPrinter(String output, int gxXPage, int gxYPage, String iniFi
138141
string idiom;
139142
if (!Config.GetValueOf("LANGUAGE", out idiom))
140143
idiom = "eng";
144+
fileContentInline = true;
141145
#if NETCORE
142146
setOuputFileName();
143147
#endif

0 commit comments

Comments
 (0)