-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1209d5
commit 3e4eef7
Showing
9 changed files
with
303 additions
and
131 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func readFile(filePath string) ([]byte, error) { | ||
_, err := os.Stat(filePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := os.ReadFile(filePath) | ||
if err != nil { | ||
return nil, fmt.Errorf("read file: %w", err) | ||
} | ||
|
||
return body, nil | ||
} | ||
|
||
func createFile(filePath string, body []byte) error { | ||
f, err := os.Create(filePath) | ||
if err != nil { | ||
return fmt.Errorf("create file: %w", err) | ||
} | ||
defer f.Close() | ||
|
||
if _, err := f.Write(body); err != nil { | ||
return fmt.Errorf("write to file: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package server | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type Method string | ||
|
||
const ( | ||
GET Method = "GET" | ||
POST Method = "POST" | ||
) | ||
|
||
type Request struct { | ||
Body []byte | ||
Headers map[string]string | ||
Method Method | ||
Target string | ||
Version string | ||
} | ||
|
||
// ParseRequest parses an HTTP request | ||
func ParseRequest(b []byte) (*Request, error) { | ||
// parse request line | ||
reader := bufio.NewReader(bytes.NewReader(b)) | ||
requestLine, err := reader.ReadString('\n') | ||
if err != nil { | ||
return nil, fmt.Errorf("read request line: %w", err) | ||
} | ||
|
||
fields := strings.Fields(requestLine) | ||
if len(fields) < 3 { | ||
return nil, fmt.Errorf("invalid request line: %s", requestLine) | ||
} | ||
method := fields[0] | ||
target := fields[1] | ||
version := fields[2] | ||
|
||
// parse headers | ||
headers := make(map[string]string) | ||
for { | ||
line, err := reader.ReadString('\n') | ||
if err != nil { | ||
return nil, fmt.Errorf("read request headers: %w", err) | ||
} | ||
if line == "\r\n" { | ||
break | ||
} | ||
|
||
splits := strings.SplitN(line, ":", 2) | ||
headers[splits[0]] = formatHeader(splits[1]) | ||
} | ||
|
||
// parse body | ||
var contentLength int | ||
contentLengthHeader := headers["Content-Length"] | ||
if contentLengthHeader != "" { | ||
contentLength, err = strconv.Atoi(headers["Content-Length"]) | ||
if err != nil { | ||
return nil, fmt.Errorf("parse content length: %w", err) | ||
} | ||
} | ||
body := make([]byte, contentLength) | ||
if _, err := io.ReadFull(reader, body); err != nil && err != io.EOF { | ||
return nil, fmt.Errorf("read request body: %w", err) | ||
} | ||
|
||
req := &Request{ | ||
Body: body, | ||
Headers: headers, | ||
Method: Method(method), | ||
Target: target, | ||
Version: version, | ||
} | ||
|
||
return req, nil | ||
} | ||
|
||
func formatHeader(val string) string { | ||
val = strings.TrimSpace(val) | ||
val = strings.Trim(val, "\r\n") | ||
return val | ||
} |
Oops, something went wrong.