Skip to content

Commit

Permalink
server: delay closing request body
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed Dec 14, 2023
1 parent dd0f53c commit 7bc96de
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package server

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"

Expand All @@ -18,8 +18,6 @@ import (
func parseInputFromRequest(r *http.Request) (file.File, error) {
src, _, err := r.FormFile("file")
if err != nil {
defer r.Body.Close()

mf, err := file.NewFileFromReader(r.Body)
if err != nil {
return nil, err
Expand All @@ -28,8 +26,6 @@ func parseInputFromRequest(r *http.Request) (file.File, error) {
return mf, nil
}

defer src.Close()

mf, err := file.NewFileFromReader(src)
if err != nil {
return nil, err
Expand Down Expand Up @@ -115,6 +111,10 @@ func getIsNewLine(r *http.Request) bool {
// 400: Bad Request
// 501: Not Implemented
func validator(w http.ResponseWriter, r *http.Request) {
if r != nil && r.Body != nil {
defer r.Body.Close()
}

metroFile, err := parseInputFromRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand All @@ -140,6 +140,10 @@ func validator(w http.ResponseWriter, r *http.Request) {
// 400: Bad Request
// 501: Not Implemented
func print(w http.ResponseWriter, r *http.Request) {
if r != nil && r.Body != nil {
defer r.Body.Close()
}

metroFile, err := parseInputFromRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand Down Expand Up @@ -172,6 +176,10 @@ func print(w http.ResponseWriter, r *http.Request) {
// 400: Bad Request
// 501: Not Implemented
func convert(w http.ResponseWriter, r *http.Request) {
if r != nil && r.Body != nil {
defer r.Body.Close()
}

metroFile, err := parseInputFromRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand Down

0 comments on commit 7bc96de

Please sign in to comment.