This commit is contained in:
Adrien Marquès 2019-05-01 21:30:16 +02:00
parent 0912e07f4f
commit 0e498607ba
4 changed files with 20 additions and 21 deletions

View File

@ -6,6 +6,18 @@ import (
"io" "io"
) )
// Reader is a multipart reader.
type Reader struct {
// io.Reader used for http.Request.Body reading
reader *bufio.Reader
// boundary used to separate multipart MultipartDatas
boundary string
// result will be inside this field
Data map[string]*Component
}
// NewReader creates a new reader from a reader and a boundary. // NewReader creates a new reader from a reader and a boundary.
func NewReader(r io.Reader, boundary string) (*Reader, error) { func NewReader(r io.Reader, boundary string) (*Reader, error) {
reader := &Reader{ reader := &Reader{
@ -21,7 +33,7 @@ func NewReader(r io.Reader, boundary string) (*Reader, error) {
} }
reader.reader = dst reader.reader = dst
// 2. Place reader after the first boundary // 2. "move" reader right after the first boundary
var err error var err error
line := make([]byte, 0) line := make([]byte, 0)
@ -40,7 +52,7 @@ func NewReader(r io.Reader, boundary string) (*Reader, error) {
// Parse parses the multipart components from the request // Parse parses the multipart components from the request
func (reader *Reader) Parse() error { func (reader *Reader) Parse() error {
/* (1) For each component (until boundary) */ // for each component (until boundary)
for { for {
comp := &Component{ comp := &Component{

View File

@ -1,9 +1,5 @@
package multipart package multipart
import (
"bufio"
)
// ConstError is a wrapper to set constant errors // ConstError is a wrapper to set constant errors
type ConstError string type ConstError string
@ -32,15 +28,3 @@ type Component struct {
// actual data // actual data
Data []byte Data []byte
} }
// Reader represents a multipart reader
type Reader struct {
// reader used for http.Request.Body reading
reader *bufio.Reader
// boundary used to separate multipart MultipartDatas
boundary string
// result will be inside this field
Data map[string]*Component
}

View File

@ -1,6 +1,7 @@
package aicra package aicra
import ( import (
"io"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -22,8 +23,10 @@ type Server struct {
// New creates a framework instance from a configuration file // New creates a framework instance from a configuration file
func New(configPath string) (*Server, error) { func New(configPath string) (*Server, error) {
var (
var err error err error
configFile io.ReadCloser
)
// 1. init instance // 1. init instance
var i = &Server{ var i = &Server{
@ -33,7 +36,7 @@ func New(configPath string) (*Server, error) {
} }
// 2. open config file // 2. open config file
configFile, err := os.Open(configPath) configFile, err = os.Open(configPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }