3
$\begingroup$

I noticed that when the response length passes around 1k characters then it is cut at weird lengths: 424, 936 or 1448 characters.

Response is generated correctly but then it is cut so if the body was eg. a JSON then it is malformed.

Any ideas?

Quiet @ Close @ listener;
responseBodyLength = 2000;

constResponse = ExportString[
   GenerateHTTPResponse @ HTTPResponse @ ExportForm[
     StringRepeat["a", responseBodyLength], "Text"
   ]
, "HTTPResponse"
];

listener = SocketListen[
   Automatic
,  With[{client = #["SourceSocket"]}, WriteString[client, constResponse]; Close@client] &
];

url = URLBuild[<|"Scheme" -> "http", 
   "Domain" -> First[listener["Socket"]["DestinationIPAddress"]], 
   "Port" -> listener["Socket"]["DestinationPort"]|>];

Test

Table[
  StringLength@URLRead[url, "Body"], 50
] // Counts
 <|936 -> 13, 1448 -> 35, 424 -> 2|>
$\endgroup$

1 Answer 1

2
$\begingroup$

It appears WriteString will return too quickly, so your message is closed in the middle of sending. In short, my current solution is to use an appropriate time to wait before Close@client (for example Pause[.05]).

On my system (version 13.2 on windows 11), the result is <|936 -> 50|>.


More details:

I've changed the message to see which part of the message is missing:

StringRiffle[Range[528], "-"]

(* Shorted Output: "1-2-3- ... -526-527-528" *)

which produces a 2003-length string similar to your case.

URLRead[url, "Body"] results in the following string:

"1-2- ... -268-26"

(* StringLength[%]: 966 instead of 2003 *)


Table[StringLength@URLRead[url, "Body"], 50] // Counts

(* Out (usually): <| 966 -> 50 |> *)

But if you add Pause[.05] before Closeing the client, everything works as expected:

StringLength@URLRead[url, "Body"]

(* Out: 2003 *)


Table[StringLength@URLRead[url, "Body"], 50] // Counts

(* Out: <| 2003 -> 50 |> *)

If we change the message to StringRiffle[Range[4000], "-"] which produces a 18892-length string, we see 966 and Pause[.05] is not enough to transfer all the message.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.