How can I tell if net/http's ResponseWriter.Write() has been called?

Flimzy

Suppose I have a chain of net/http Handlers, and an early one responds with an HTTP error (http.StatusInternalServerError, for instance). How can I detect this in the following handlers, and avoid sending additional data to the client?

Or is this entirely the wrong approach to the problem?

Ainar-G

http.ResponseWriter is an interface. So just compose a new instance of it:

type MyResponseWriter struct {
    http.ResponseWriter
    WroteHeader bool
}

func (w *MyResponseWriter) Write(b []byte) (int, error) {
    w.WroteHeader = true
    return w.ResponseWriter.Write(b)
}

func (w *MyResponseWriter) WriteHeader(code int) {
    w.WroteHeader = true
    w.ResponseWriter.WriteHeader(code)
}

And in your handlers:

//...
if w, ok := w.(*MyResponseWriter); ok && w.WroteHeader {
    log.Println("Already wrote, skipping")
    return
}

EDIT: Another thing to consider. Most of the time if you have a "chain" of handlers that means that a handler is called inside a handler. So if you have something like

type Handler1 struct { http.Handler }
type Handler2 struct { http.Handler }
type Handler3 struct { http.Handler }
var MyHandler http.Handler = Handler1{Handler2{Handler3{h}}}

as long as each of those call the inner handler as the last thing they do with w and r, you should be fine because then w and r won't even reach the inner handler. E.g.

func (h Handler2) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    if somethingBadHappened() {
        w.WriteHeader(http.StatusInternalServerError)
        return
    }
    h.ServeHTTP(w, r) // Not called if somethingBadHappened().
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I tell if a CVE has been fixed in Ubuntu's repositories?

From Dev

How can I check if an anonymous function has been called with NSubstitute?

From Java

How can I test that a function has not been called?

From Dev

How can I verify that RemoveRange has been called on a mock DbContext?

From Dev

How can I point to a member of a std::set in such a way that I can tell if the element has been removed?

From Dev

Can I setup Git to tell me each time a certain file(s) has been modified

From Dev

How can I tell how long a Jenkins job has been in the wait queue?

From Dev

Looking at git diff --name-status, how can I tell that a file has been renamed?

From Dev

How can I tell if a jquery ui dialog query has been initialized?

From Dev

How Can I Tell if My struct tm Has Been Left in an Invalid State?

From Dev

In Django Celery how can I tell if a task has been executed asynchronously

From Dev

How can I tell when AJAX has been intentionally canceled by the browser?

From Dev

When making a call from the browser, how can I tell programmatically when the call has been answered?

From Dev

How can I tell if my memory has been released securely by a PHP application?

From Dev

How can I tell if a fortran array pointer has been allocated directly, or is associated with another object?

From Dev

When making a call from the browser, how can I tell programmatically when the call has been answered?

From Dev

How can I tell if a specific button in a Bootstrap Modal has been clicked in my JQuery function?

From Dev

How can I use a Raycast to tell when a child game object has been mouse clicked

From Dev

How can I tell if vim was called with a filename?

From Dev

How can I add a mapping in AutoMapper after Initialize has been called?

From Dev

How can I know that onCreateView has been called from an outer class?

From Dev

How can I verify that a method has been called with intern test framework?

From Dev

How can I repeat functions in F# after a function has been called inside it?

From Dev

How can I tell if a machine has PAE?

From Dev

How would I go about adding a button id so I can tell which button has been pressed in Rails

From Dev

a socket fd has been called "shutdown", can I "reopen" it?

From Dev

How can I make vim only write to file after buffer has been modified?

From Dev

How can I tell when documents have been indexed?

From Dev

How do I return an image after it has been called?

Related Related

  1. 1

    How can I tell if a CVE has been fixed in Ubuntu's repositories?

  2. 2

    How can I check if an anonymous function has been called with NSubstitute?

  3. 3

    How can I test that a function has not been called?

  4. 4

    How can I verify that RemoveRange has been called on a mock DbContext?

  5. 5

    How can I point to a member of a std::set in such a way that I can tell if the element has been removed?

  6. 6

    Can I setup Git to tell me each time a certain file(s) has been modified

  7. 7

    How can I tell how long a Jenkins job has been in the wait queue?

  8. 8

    Looking at git diff --name-status, how can I tell that a file has been renamed?

  9. 9

    How can I tell if a jquery ui dialog query has been initialized?

  10. 10

    How Can I Tell if My struct tm Has Been Left in an Invalid State?

  11. 11

    In Django Celery how can I tell if a task has been executed asynchronously

  12. 12

    How can I tell when AJAX has been intentionally canceled by the browser?

  13. 13

    When making a call from the browser, how can I tell programmatically when the call has been answered?

  14. 14

    How can I tell if my memory has been released securely by a PHP application?

  15. 15

    How can I tell if a fortran array pointer has been allocated directly, or is associated with another object?

  16. 16

    When making a call from the browser, how can I tell programmatically when the call has been answered?

  17. 17

    How can I tell if a specific button in a Bootstrap Modal has been clicked in my JQuery function?

  18. 18

    How can I use a Raycast to tell when a child game object has been mouse clicked

  19. 19

    How can I tell if vim was called with a filename?

  20. 20

    How can I add a mapping in AutoMapper after Initialize has been called?

  21. 21

    How can I know that onCreateView has been called from an outer class?

  22. 22

    How can I verify that a method has been called with intern test framework?

  23. 23

    How can I repeat functions in F# after a function has been called inside it?

  24. 24

    How can I tell if a machine has PAE?

  25. 25

    How would I go about adding a button id so I can tell which button has been pressed in Rails

  26. 26

    a socket fd has been called "shutdown", can I "reopen" it?

  27. 27

    How can I make vim only write to file after buffer has been modified?

  28. 28

    How can I tell when documents have been indexed?

  29. 29

    How do I return an image after it has been called?

HotTag

Archive