vendoring and caldav
This commit is contained in:
parent
a06aa900b7
commit
fd9092e5ab
96 changed files with 14832 additions and 5 deletions
65
vendor/github.com/samedi/caldav-go/handlers/response.go
generated
vendored
Normal file
65
vendor/github.com/samedi/caldav-go/handlers/response.go
generated
vendored
Normal file
|
@ -0,0 +1,65 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"github.com/samedi/caldav-go/errs"
|
||||
)
|
||||
|
||||
type Response struct {
|
||||
Status int
|
||||
Header http.Header
|
||||
Body string
|
||||
Error error
|
||||
}
|
||||
|
||||
func NewResponse() *Response {
|
||||
return &Response{
|
||||
Header: make(http.Header),
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Response) Set(status int, body string) *Response {
|
||||
this.Status = status
|
||||
this.Body = body
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *Response) SetHeader(key, value string) *Response {
|
||||
this.Header.Set(key, value)
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *Response) SetError(err error) *Response {
|
||||
this.Error = err
|
||||
|
||||
switch err {
|
||||
case errs.ResourceNotFoundError:
|
||||
this.Status = http.StatusNotFound
|
||||
case errs.UnauthorizedError:
|
||||
this.Status = http.StatusUnauthorized
|
||||
case errs.ForbiddenError:
|
||||
this.Status = http.StatusForbidden
|
||||
default:
|
||||
this.Status = http.StatusInternalServerError
|
||||
}
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
func (this *Response) Write(writer http.ResponseWriter) {
|
||||
if this.Error == errs.UnauthorizedError {
|
||||
this.SetHeader("WWW-Authenticate", `Basic realm="Restricted"`)
|
||||
}
|
||||
|
||||
for key, values := range this.Header {
|
||||
for _, value := range values {
|
||||
writer.Header().Set(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
writer.WriteHeader(this.Status)
|
||||
io.WriteString(writer, this.Body)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue