Skip to content

Commit

Permalink
Cleanup response handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vvidic committed Jul 20, 2020
1 parent 0f00b2f commit f23d9ec
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions mjpeg-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,13 @@ func (chunker *Chunker) Connect() error {
}

if resp.StatusCode != http.StatusOK {
defer func() {
err := resp.Body.Close()
if err != nil {
fmt.Printf("chunker[%s]: body close failed: %s\n", chunker.id, err)
}
}()
chunker.closeResponse(resp)
return fmt.Errorf("request failed: %s", resp.Status)
}

boundary, err := getBoundary(*resp)
boundary, err := getBoundary(resp)
if err != nil {
defer func() {
err := resp.Body.Close()
if err != nil {
fmt.Printf("chunker[%s]: body close failed: %s\n", chunker.id, err)
}
}()
chunker.closeResponse(resp)
return err
}

Expand All @@ -123,7 +113,14 @@ func (chunker *Chunker) Connect() error {
return nil
}

func getBoundary(resp http.Response) (string, error) {
func (chunker *Chunker) closeResponse(resp *http.Response) {
err := resp.Body.Close()
if err != nil {
fmt.Printf("chunker[%s]: body close failed: %s\n", chunker.id, err)
}
}

func getBoundary(resp *http.Response) (string, error) {
contentType := resp.Header.Get("Content-Type")
mediaType, params, err := mime.ParseMediaType(contentType)
if err != nil {
Expand Down

0 comments on commit f23d9ec

Please sign in to comment.