net/mime
The media type of a file, the Content-Type header, and multipart form bodies.
import net.mime
A media type (MIME) tells the browser and other programs what the content is, such as image/png. This package finds the type from a file extension and reads the Content-Type header.
Forms that upload a file send a multipart/form-data body: several parts with a boundary between them. ParseMultipart reads one and MultipartWriter builds one.
Types
ContentTypeParam
struct ContentTypeParam| Name | Type | Description |
|---|---|---|
key | str = "" | The name, in lower case. |
value | str = "" | The value. |
import net.mime
func main:
println mime.MimeExt("png"), mime.MimeExt("json"), mime.MimeExt(".png")
println mime.MimeType("/site/index.html"), mime.MimeType("style.css")
println mime.StatusText(200), mime.StatusText(404), mime.StatusText(503)
ct := mime.ParseContentType("text/html; charset=UTF-8; q=0.9")
println ct.media_type, ct.params.items.len(), mime.ParamGet(ct, "charset"), ct.params.items[1].key, ct.params.items[1].value
endContentTypeParams
struct ContentTypeParams| Name | Type | Description |
|---|---|---|
items | Vector | The parameters. |
The example for this item is shown with the ones above: net-mime-Types.salam
ContentType
struct ContentTypeContent-Type header.| Name | Type | Description |
|---|---|---|
media_type | str = "" | The media type, in lower case. |
params | ContentTypeParams | The parameters, such as charset. |
The example for this item is shown with the ones above: net-mime-Types.salam
MultipartHeader
struct MultipartHeader| Name | Type | Description |
|---|---|---|
key | str = "" | The name. |
value | str = "" | The value. |
import net.mime
import compress
import str
func main:
mut w := mime.NewMultipartWriter()
println str.StartsWith(mime.ContentTypeHeader(w), "multipart/form-data; boundary=")
mime.WriteField(w, "name", "Maryam")
mime.WriteFileStr(w, "note", "note.txt", "text/plain", "hello")
data := compress.BufFromStr("PNG")
mime.WriteFileBytes(w, "pic", "a.png", "image/png", data.data, data.len)
body := mime.CloseWriter(w)
println w.closed, body.len > 0
parts := mime.ParseMultipart(body.data, body.len, w.boundary)
each p in parts.items:
println p.name, "[" + p.filename + "]", "[" + p.content_type + "]", compress.BufToStrUnsafe(p.body), p.headers.items.len()
end
println parts.items[1].headers.items[0].key
endMultipartHeaders
struct MultipartHeaders| Name | Type | Description |
|---|---|---|
items | Vector | The headers. |
The example for this item is shown with the ones above: net-mime-Multipart.salam
MultipartPart
struct MultipartPart| Name | Type | Description |
|---|---|---|
name | str = "" | The name of the form field. |
filename | str = "" | The file name, when the part is a file. |
content_type | str = "" | The content type of the part. |
headers | MultipartHeaders | Every header of the part. |
body | compress.ByteBuf | The contents of the part. |
The example for this item is shown with the ones above: net-mime-Multipart.salam
MultipartParts
struct MultipartParts| Name | Type | Description |
|---|---|---|
items | Vector | The parts. |
The example for this item is shown with the ones above: net-mime-Multipart.salam
MultipartWriter
struct MultipartWriter| Name | Type | Description |
|---|---|---|
boundary | str = "" | The random boundary between the parts. |
buf | compress.ByteBuf | The body built so far. |
closed | bool = false | Whether it has been closed. |
The example for this item is shown with the ones above: net-mime-Multipart.salam
Functions
MimeExt
func MimeExt(ext: str): strpng; application/octet-stream for an unknown extension.The example for this item is shown with the ones above: net-mime-Types.salam
MimeType
func MimeType(p: str): strThe example for this item is shown with the ones above: net-mime-Types.salam
StatusText
func StatusText(code: int): strNot Found for 404.The example for this item is shown with the ones above: net-mime-Types.salam
ParseContentType
func ParseContentType(header: str): ContentTypeContent-Type header.The example for this item is shown with the ones above: net-mime-Types.salam
ParamGet
func ParamGet(ct: ContentType, key: str): strThe example for this item is shown with the ones above: net-mime-Types.salam
ParseMultipart
func ParseMultipart(data: void*, n: int, boundary: str): MultipartPartsmultipart/form-data body into parts at the given boundary; for taking in uploaded files.The example for this item is shown with the ones above: net-mime-Multipart.salam
NewMultipartWriter
func NewMultipartWriter(): MultipartWriterThe example for this item is shown with the ones above: net-mime-Multipart.salam
ContentTypeHeader
func ContentTypeHeader(w: MultipartWriter): strContent-Type header value to send with this body.The example for this item is shown with the ones above: net-mime-Multipart.salam
WriteField
func WriteField(w &: MultipartWriter, name: str, value: str)The example for this item is shown with the ones above: net-mime-Multipart.salam
WriteFileBytes
func WriteFileBytes(w &: MultipartWriter, name: str, filename: str, content_type: str, data: void*, n: int)The example for this item is shown with the ones above: net-mime-Multipart.salam
WriteFileStr
func WriteFileStr(w &: MultipartWriter, name: str, filename: str, content_type: str, data: str)The example for this item is shown with the ones above: net-mime-Multipart.salam
CloseWriter
func CloseWriter(w &: MultipartWriter): compress.ByteBufThe example for this item is shown with the ones above: net-mime-Multipart.salam