Created
November 19, 2017 11:31
-
-
Save Fudoshiki/8811d6a9afae97d3002cd006f5808309 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defp fetch_dev_server path do | |
case :gen_tcp.connect {:local, "/tmp/webpack"}, 0, [:binary, active: false, packet: 0] do | |
{:ok, socket} -> | |
:gen_tcp.send socket, "GET /#{path}.css HTTP/1.1\r\n\r\n" | |
{:ok, response} = :gen_tcp.recv socket, 0 | |
length = List.last(Regex.run ~r{Content-Length: (\d+)}, response) |> String.to_integer | |
start_bin = Enum.slice String.split(response, "\r\n"), 7..-1 | |
case length <= 1460 do | |
true -> | |
:gen_tcp.close socket | |
start_bin | |
_ -> | |
:gen_tcp.send socket, "GET /#{path}.css HTTP/1.1\r\nConnection: close\r\n\r\n" | |
{:ok, end_bin} = :gen_tcp.recv socket, length - 1460 | |
start_bin ++ end_bin | |
end | |
{:error, :enoent} -> | |
:timer.sleep 1000 | |
fetch_dev_server path | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment