79 lines
1.5 KiB
Odin
79 lines
1.5 KiB
Odin
package http
|
|
|
|
import "core:strconv"
|
|
|
|
RequestError :: enum {
|
|
InvalidRequestLine,
|
|
InvalidMethod,
|
|
InvalidHeaderFormat,
|
|
IncompleteRequest,
|
|
ContentLengthMismatch,
|
|
FailedParsing,
|
|
NetworkError,
|
|
}
|
|
|
|
ResponseError :: enum {
|
|
InvalidContentType,
|
|
InvalidStatus,
|
|
}
|
|
|
|
Header :: map[string]string
|
|
|
|
Status :: enum u32 {
|
|
Ok = 200,
|
|
BadRequest = 400,
|
|
NotFound = 404,
|
|
InternalServerError = 500,
|
|
}
|
|
|
|
ContentType :: enum {
|
|
Html,
|
|
Image,
|
|
}
|
|
|
|
Response :: struct {
|
|
header: Header,
|
|
proto_version: string,
|
|
status: Status,
|
|
body: []byte,
|
|
}
|
|
|
|
Method :: enum {
|
|
GET,
|
|
POST,
|
|
PUT,
|
|
DELETE,
|
|
}
|
|
|
|
Request :: struct {
|
|
header: Header,
|
|
proto_version: string,
|
|
method: Method,
|
|
path: string,
|
|
body: []byte,
|
|
path_variables: map[string]string,
|
|
}
|
|
|
|
make_response :: proc(
|
|
status: Status,
|
|
body: []byte,
|
|
content_type: ContentType,
|
|
allocator := context.temp_allocator,
|
|
) -> Response {
|
|
response := Response {
|
|
status = status,
|
|
proto_version = "HTTP/1.1",
|
|
header = make(Header, allocator),
|
|
body = body,
|
|
}
|
|
|
|
buf := make([]byte, 32, allocator)
|
|
response.header["Content-Length"] = strconv.write_uint(
|
|
buf[:],
|
|
u64(len(body)),
|
|
10,
|
|
)
|
|
response.header["Content-Type"] = str_from_content_type(content_type)
|
|
|
|
return response
|
|
}
|