2024-03-14 03:08:53 +05:30
|
|
|
package dataformat
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"mime"
|
|
|
|
|
"mime/multipart"
|
2024-10-10 20:22:22 +05:30
|
|
|
"net/textproto"
|
2024-03-25 10:08:26 +05:30
|
|
|
|
|
|
|
|
mapsutil "github.com/projectdiscovery/utils/maps"
|
2024-03-14 03:08:53 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type MultiPartForm struct {
|
2024-10-10 20:22:22 +05:30
|
|
|
boundary string
|
|
|
|
|
filesMetadata map[string]FileMetadata
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type FileMetadata struct {
|
|
|
|
|
ContentType string
|
|
|
|
|
Filename string
|
2024-03-14 03:08:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
_ DataFormat = &MultiPartForm{}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// NewMultiPartForm returns a new MultiPartForm encoder
|
|
|
|
|
func NewMultiPartForm() *MultiPartForm {
|
|
|
|
|
return &MultiPartForm{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsType returns true if the data is MultiPartForm encoded
|
|
|
|
|
func (m *MultiPartForm) IsType(data string) bool {
|
|
|
|
|
// This method should be implemented to detect if the data is multipart form encoded
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Encode encodes the data into MultiPartForm format
|
2024-03-25 10:08:26 +05:30
|
|
|
func (m *MultiPartForm) Encode(data KV) (string, error) {
|
2024-03-14 03:08:53 +05:30
|
|
|
var b bytes.Buffer
|
|
|
|
|
w := multipart.NewWriter(&b)
|
|
|
|
|
if err := w.SetBoundary(m.boundary); err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-25 10:08:26 +05:30
|
|
|
var Itererr error
|
|
|
|
|
data.Iterate(func(key string, value any) bool {
|
2024-03-14 03:08:53 +05:30
|
|
|
var fw io.Writer
|
|
|
|
|
var err error
|
2024-10-10 20:22:22 +05:30
|
|
|
|
2025-08-25 15:12:51 +07:00
|
|
|
if fileMetadata, ok := m.filesMetadata[key]; ok {
|
|
|
|
|
if filesArray, isArray := value.([]any); isArray {
|
|
|
|
|
for _, file := range filesArray {
|
|
|
|
|
h := make(textproto.MIMEHeader)
|
|
|
|
|
h.Set("Content-Disposition",
|
|
|
|
|
fmt.Sprintf(`form-data; name=%q; filename=%q`,
|
|
|
|
|
key, fileMetadata.Filename))
|
|
|
|
|
h.Set("Content-Type", fileMetadata.ContentType)
|
2024-10-10 20:22:22 +05:30
|
|
|
|
2025-08-25 15:12:51 +07:00
|
|
|
if fw, err = w.CreatePart(h); err != nil {
|
|
|
|
|
Itererr = err
|
|
|
|
|
return false
|
|
|
|
|
}
|
2024-10-10 20:22:22 +05:30
|
|
|
|
2025-08-25 15:12:51 +07:00
|
|
|
if _, err = fw.Write([]byte(file.(string))); err != nil {
|
|
|
|
|
Itererr = err
|
|
|
|
|
return false
|
|
|
|
|
}
|
2024-10-10 20:22:22 +05:30
|
|
|
}
|
|
|
|
|
|
2025-08-25 15:12:51 +07:00
|
|
|
return true
|
2024-10-10 20:22:22 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-14 03:08:53 +05:30
|
|
|
// Add field
|
2025-08-25 15:12:51 +07:00
|
|
|
var values []string
|
|
|
|
|
switch v := value.(type) {
|
|
|
|
|
case nil:
|
|
|
|
|
values = []string{""}
|
|
|
|
|
case string:
|
|
|
|
|
values = []string{v}
|
|
|
|
|
case []string:
|
|
|
|
|
values = v
|
|
|
|
|
case []any:
|
|
|
|
|
values = make([]string, len(v))
|
|
|
|
|
for i, item := range v {
|
|
|
|
|
if item == nil {
|
|
|
|
|
values[i] = ""
|
|
|
|
|
} else {
|
|
|
|
|
values[i] = fmt.Sprint(item)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
values = []string{fmt.Sprintf("%v", v)}
|
2024-03-14 03:08:53 +05:30
|
|
|
}
|
2024-10-10 20:22:22 +05:30
|
|
|
|
2025-08-25 15:12:51 +07:00
|
|
|
for _, val := range values {
|
|
|
|
|
if fw, err = w.CreateFormField(key); err != nil {
|
|
|
|
|
Itererr = err
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if _, err = fw.Write([]byte(val)); err != nil {
|
|
|
|
|
Itererr = err
|
|
|
|
|
return false
|
|
|
|
|
}
|
2024-03-14 03:08:53 +05:30
|
|
|
}
|
2024-03-25 10:08:26 +05:30
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
if Itererr != nil {
|
|
|
|
|
return "", Itererr
|
2024-03-14 03:08:53 +05:30
|
|
|
}
|
|
|
|
|
|
2025-07-01 00:40:44 +07:00
|
|
|
_ = w.Close()
|
2024-03-14 03:08:53 +05:30
|
|
|
return b.String(), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ParseBoundary parses the boundary from the content type
|
|
|
|
|
func (m *MultiPartForm) ParseBoundary(contentType string) error {
|
|
|
|
|
_, params, err := mime.ParseMediaType(contentType)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
m.boundary = params["boundary"]
|
|
|
|
|
if m.boundary == "" {
|
|
|
|
|
return fmt.Errorf("no boundary found in the content type")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Decode decodes the data from MultiPartForm format
|
2024-03-25 10:08:26 +05:30
|
|
|
func (m *MultiPartForm) Decode(data string) (KV, error) {
|
2024-03-14 03:08:53 +05:30
|
|
|
// Create a buffer from the string data
|
|
|
|
|
b := bytes.NewBufferString(data)
|
|
|
|
|
// The boundary parameter should be extracted from the Content-Type header of the HTTP request
|
|
|
|
|
// which is not available in this context, so this is a placeholder for demonstration.
|
|
|
|
|
// You will need to pass the actual boundary value to this function.
|
|
|
|
|
r := multipart.NewReader(b, m.boundary)
|
|
|
|
|
|
|
|
|
|
form, err := r.ReadForm(32 << 20) // 32MB is the max memory used to parse the form
|
|
|
|
|
if err != nil {
|
2024-03-25 10:08:26 +05:30
|
|
|
return KV{}, err
|
2024-03-14 03:08:53 +05:30
|
|
|
}
|
|
|
|
|
defer func() {
|
|
|
|
|
_ = form.RemoveAll()
|
|
|
|
|
}()
|
|
|
|
|
|
2024-03-25 10:08:26 +05:30
|
|
|
result := mapsutil.NewOrderedMap[string, any]()
|
2024-03-14 03:08:53 +05:30
|
|
|
for key, values := range form.Value {
|
|
|
|
|
if len(values) > 1 {
|
2024-03-25 10:08:26 +05:30
|
|
|
result.Set(key, values)
|
2024-03-14 03:08:53 +05:30
|
|
|
} else {
|
2024-03-25 10:08:26 +05:30
|
|
|
result.Set(key, values[0])
|
2024-03-14 03:08:53 +05:30
|
|
|
}
|
|
|
|
|
}
|
2024-10-10 20:22:22 +05:30
|
|
|
m.filesMetadata = make(map[string]FileMetadata)
|
2024-03-14 03:08:53 +05:30
|
|
|
for key, files := range form.File {
|
|
|
|
|
fileContents := []interface{}{}
|
|
|
|
|
for _, fileHeader := range files {
|
|
|
|
|
file, err := fileHeader.Open()
|
|
|
|
|
if err != nil {
|
2024-03-25 10:08:26 +05:30
|
|
|
return KV{}, err
|
2024-03-14 03:08:53 +05:30
|
|
|
}
|
2025-07-01 00:40:44 +07:00
|
|
|
defer func() {
|
2025-07-09 14:47:26 -05:00
|
|
|
_ = file.Close()
|
|
|
|
|
}()
|
2024-03-14 03:08:53 +05:30
|
|
|
|
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
|
if _, err := buffer.ReadFrom(file); err != nil {
|
2024-03-25 10:08:26 +05:30
|
|
|
return KV{}, err
|
2024-03-14 03:08:53 +05:30
|
|
|
}
|
|
|
|
|
fileContents = append(fileContents, buffer.String())
|
2024-10-10 20:22:22 +05:30
|
|
|
|
|
|
|
|
m.filesMetadata[key] = FileMetadata{
|
|
|
|
|
ContentType: fileHeader.Header.Get("Content-Type"),
|
|
|
|
|
Filename: fileHeader.Filename,
|
|
|
|
|
}
|
2024-03-14 03:08:53 +05:30
|
|
|
}
|
2024-03-25 10:08:26 +05:30
|
|
|
result.Set(key, fileContents)
|
2024-03-14 03:08:53 +05:30
|
|
|
}
|
2024-03-25 10:08:26 +05:30
|
|
|
return KVOrderedMap(&result), nil
|
2024-03-14 03:08:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Name returns the name of the encoder
|
|
|
|
|
func (m *MultiPartForm) Name() string {
|
|
|
|
|
return "multipart/form-data"
|
|
|
|
|
}
|