Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Problemas ao responder arquivos estáticos #379

Open
willianbnu opened this issue Feb 27, 2024 · 1 comment
Open

Problemas ao responder arquivos estáticos #379

willianbnu opened this issue Feb 27, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@willianbnu
Copy link

Bom dia !

Estamos utilizando o horse para responder arquivos estáticos para a pagina de origem, ao qual respondemos enviando a Stream no Response.

Mas como temos referencia a 10+ arquivos, aleatoriamente, ocorre a exceção: EIdSocketError Software caused connection abort, e mesmo as vezes não ocorrendo este erro, também aleatoriamente, alguns dos arquivos estáticos não são carregados e não ocorre erro algum.

Tentamos utilizar o middleware ServerStatic, mas ocorre a mesma situação.

image

Abaixo segue o fonte implementado:

.Use( procedure (Req: THorseRequest; Res: THorseResponse; Next: TProc) begin // Verifique se a solicitação é para um arquivo estático if StartsText('/Paginas/', Req.RawWebRequest.PathInfo) then begin var FileName := ReplaceStr(ExtractFilePath(ParamStr(0)) + Req.RawWebRequest.PathInfo,'/','\'); // Verifique se o arquivo solicitado existe if FileExists(FileName) then begin // Abra o arquivo solicitado e envie seu conteúdo como resposta var FileStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone); try Res.RawWebResponse.ContentType := MIMETypeFromFile(FileName); //Delimita quanto tempo o arquivo vai ser mantido em cache em segundos Res.RawWebResponse.SetCustomHeader('Cache-Control', 'max-age=3600'); Res.RawWebResponse.ContentStream := FileStream; Res.RawWebResponse.StatusCode := 200; Res.Status(THTTPStatus.OK); Res.RawWebResponse.SendResponse; except on E: Exception do begin FileStream.Free; Res.RawWebResponse.StatusCode := 500; Res.Status(THTTPStatus.InternalServerError); Res.Send('Erro ao servir arquivo estático: ' + E.Message); end; end; end else begin // Se o arquivo não existir, retorne um erro 404 (Não encontrado) Res.RawWebResponse.StatusCode := 404; Res.Status(THTTPStatus.NotFound); Res.Send('Arquivo não encontrado'); end; end; end)

@viniciussanchez
Copy link
Member

Bom dia... conseguiu resolver o problema?
Se for possível, nos envie um projeto de exemplo compactado para que possamos analisar por favor...

@viniciussanchez viniciussanchez added the bug Something isn't working label May 8, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants