Standard library · Network

net/mime

The media type of a file, the Content-Type header, and multipart form bodies.

import net.mime
On this page
  1. Types
  2. Functions

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
One parameter.
NameTypeDescription
keystr = ""The name, in lower case.
valuestr = ""The value.
net-mime-Types.salam
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
end
Outputimage/png application/json application/octet-stream text/html; charset=utf-8 text/css OK Not Found Service Unavailable text/html 2 UTF-8 q 0.9

ContentTypeParams

struct ContentTypeParams
A list of parameters.
NameTypeDescription
itemsVectorThe parameters.

The example for this item is shown with the ones above: net-mime-Types.salam

ContentType

struct ContentType
A parsed Content-Type header.
NameTypeDescription
media_typestr = ""The media type, in lower case.
paramsContentTypeParamsThe parameters, such as charset.

The example for this item is shown with the ones above: net-mime-Types.salam

MultipartHeader

struct MultipartHeader
One header of a part.
NameTypeDescription
keystr = ""The name.
valuestr = ""The value.
net-mime-Multipart.salam
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
end
Outputtrue true true name [] [] Maryam 1 note [note.txt] [text/plain] hello 2 pic [a.png] [image/png] PNG 2 Content-Disposition

MultipartHeaders

struct MultipartHeaders
A list of headers.
NameTypeDescription
itemsVectorThe headers.

The example for this item is shown with the ones above: net-mime-Multipart.salam

MultipartPart

struct MultipartPart
One part of a multipart body.
NameTypeDescription
namestr = ""The name of the form field.
filenamestr = ""The file name, when the part is a file.
content_typestr = ""The content type of the part.
headersMultipartHeadersEvery header of the part.
bodycompress.ByteBufThe contents of the part.

The example for this item is shown with the ones above: net-mime-Multipart.salam

MultipartParts

struct MultipartParts
A list of parts.
NameTypeDescription
itemsVectorThe parts.

The example for this item is shown with the ones above: net-mime-Multipart.salam

MultipartWriter

struct MultipartWriter
A builder of a multipart body.
NameTypeDescription
boundarystr = ""The random boundary between the parts.
bufcompress.ByteBufThe body built so far.
closedbool = falseWhether 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): str
The media type for a file extension, without the dot and in lower case, such as png; 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): str
The media type from a file path.

The example for this item is shown with the ones above: net-mime-Types.salam

StatusText

func StatusText(code: int): str
The standard text of an HTTP status code, such as Not Found for 404.

The example for this item is shown with the ones above: net-mime-Types.salam

ParseContentType

func ParseContentType(header: str): ContentType
Parses a Content-Type header.

The example for this item is shown with the ones above: net-mime-Types.salam

ParamGet

func ParamGet(ct: ContentType, key: str): str
The value of one parameter; an empty string when there is none.

The example for this item is shown with the ones above: net-mime-Types.salam

ParseMultipart

func ParseMultipart(data: void*, n: int, boundary: str): MultipartParts
Splits a multipart/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(): MultipartWriter
A new builder with a random boundary.

The example for this item is shown with the ones above: net-mime-Multipart.salam

ContentTypeHeader

func ContentTypeHeader(w: MultipartWriter): str
The Content-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)
Adds one text form field.

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)
Adds one file from memory.

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)
Adds one text file.

The example for this item is shown with the ones above: net-mime-Multipart.salam

CloseWriter

func CloseWriter(w &: MultipartWriter): compress.ByteBuf
Writes the closing boundary and returns the complete body.

The example for this item is shown with the ones above: net-mime-Multipart.salam