Skip to content

http #

Constants #

const status_service_unavailable = vhttp.Status.service_unavailable
const status_bad_gateway = vhttp.Status.bad_gateway
const status_not_implemented = vhttp.Status.not_implemented
const status_internal_server_error = vhttp.Status.internal_server_error
const status_unprocessable_entity = vhttp.Status.unprocessable_entity
const status_conflict = vhttp.Status.conflict
const status_method_not_allowed = vhttp.Status.method_not_allowed
const status_not_found = vhttp.Status.not_found
const status_forbidden = vhttp.Status.forbidden
const status_unauthorized = vhttp.Status.unauthorized
const status_bad_request = vhttp.Status.bad_request
const status_permanent_redirect = vhttp.Status.permanent_redirect
const status_temporary_redirect = vhttp.Status.temporary_redirect
const status_not_modified = vhttp.Status.not_modified
const status_see_other = vhttp.Status.see_other
const status_found = vhttp.Status.found
const status_moved_permanently = vhttp.Status.moved_permanently
const status_no_content = vhttp.Status.no_content
const status_accepted = vhttp.Status.accepted
const status_created = vhttp.Status.created
const status_ok = vhttp.Status.ok

Common status code constants for convenience

const same_site_none = 'None'
const same_site_lax = 'Lax'
const same_site_strict = 'Strict'

Common same-site values

fn cleanup_multipart_form #

fn cleanup_multipart_form(form &MultipartForm)

cleanup_multipart_form removes all temporary files from a form

fn cleanup_uploaded_file #

fn cleanup_uploaded_file(file &UploadedFile)

cleanup_uploaded_file removes the temporary file

fn error_page #

fn error_page(status_code int, message string) string

error_page generates a simple HTML error page

fn http_status_text #

fn http_status_text(code int) string

http_status_text returns the standard HTTP status text for a code

fn new_context #

fn new_context(raw &Request) Context

Create context from V request

fn new_context_with_db #

fn new_context_with_db(raw &Request, renderer &templates.Renderer, db &vareldb.DB) Context

Create context with template renderer and database connection

fn new_context_with_renderer #

fn new_context_with_renderer(raw &Request, renderer &templates.Renderer) Context

Create context with template renderer

fn new_multipart_form #

fn new_multipart_form() MultipartForm

Create a new empty multipart form

fn new_request #

fn new_request(raw &Request) Request

new_request creates Request from V's http.Request

fn new_response #

fn new_response() Response

Constructors

fn ok #

fn ok(body string) Response

fn parse_cookies #

fn parse_cookies(header_value string) map[string]string

Parse cookies from request header Simple parser for Cookie header format: "name1=value1; name2=value2"

fn parse_multipart #

fn parse_multipart(req &Request, config UploadConfig) !MultipartForm

Parse multipart/form-data from request body

fn save_uploaded_file #

fn save_uploaded_file(file &UploadedFile, dest_path string) !

save_uploaded_file moves an uploaded file to a permanent location

fn status_from_int #

fn status_from_int(code int) Status

fn status_text #

fn status_text(status Status) string

Status helpers

type HandlerFunc #

type HandlerFunc = fn (mut ctx Context) Response

HandlerFunc is the function signature for route handlers Takes a mutable Context and returns a Response

type Status #

type Status = vhttp.Status

Re-export V's Status enum for convenience

struct Context #

struct Context {
pub mut:
	req      Request
	resp     Response
	params   map[string]string // Route parameters from router
	values   map[string]string // Middleware data storage
	session  &session.Session    = unsafe { nil } // Session data (set by session middleware)
	renderer &templates.Renderer = unsafe { nil } // Template renderer (optional)
	db       &vareldb.DB         = unsafe { nil } // Database connection (worker-specific)
}

Context unifies Request and Response for handlers

fn (Context) asset_path #

fn (ctx &Context) asset_path(file_path string) string

asset_path generates a path to a static asset in the public directory

Examples

ctx.asset_path('css/main.css') -> '/public/css/main.css'
ctx.asset_path('images/logo.png') -> '/public/images/logo.png'

fn (Context) bad_request #

fn (mut ctx Context) bad_request(message string) Response

fn (Context) cookie #

fn (ctx &Context) cookie(name string) string

fn (Context) created #

fn (mut ctx Context) created[T](data T) Response

fn (Context) current_email #

fn (ctx &Context) current_email() string

Get current authenticated user's email

fn (Context) current_user_id #

fn (ctx &Context) current_user_id() ?int

Get current authenticated user's ID

fn (Context) current_username #

fn (ctx &Context) current_username() string

Get current authenticated user's username

fn (Context) file #

fn (mut ctx Context) file(field_name string) ?UploadedFile

file returns the first uploaded file for a field name

fn (Context) files #

fn (mut ctx Context) files(field_name string) []UploadedFile

files returns all uploaded files for a field name

fn (Context) forbidden #

fn (mut ctx Context) forbidden(message string) Response

fn (Context) form #

fn (mut ctx Context) form(key string) string

fn (Context) form_all #

fn (mut ctx Context) form_all(key string) []string

fn (Context) full_url #

fn (ctx &Context) full_url(path string) string

full_url generates a complete URL including scheme, host, and path

Example

ctx.full_url('/users/123') -> 'http://localhost:8080/users/123'

fn (Context) get #

fn (ctx &Context) get(key string) string

fn (Context) has #

fn (ctx &Context) has(key string) bool

fn (Context) has_current_user #

fn (ctx &Context) has_current_user() bool

Check if current user is authenticated (alternative to is_authenticated)

fn (Context) has_file #

fn (ctx &Context) has_file(field_name string) bool

has_file checks if a file was uploaded for a field

fn (Context) has_param #

fn (ctx &Context) has_param(key string) bool

fn (Context) has_query #

fn (ctx &Context) has_query(key string) bool

fn (Context) has_session #

fn (ctx &Context) has_session() bool

Session helper methods (require session middleware)

fn (Context) header #

fn (ctx &Context) header(key string) string

fn (Context) html #

fn (mut ctx Context) html(status_code int, body string) Response

fn (Context) internal_error #

fn (mut ctx Context) internal_error(message string) Response

fn (Context) is_authenticated #

fn (ctx &Context) is_authenticated() bool

fn (Context) is_delete #

fn (ctx &Context) is_delete() bool

fn (Context) is_get #

fn (ctx &Context) is_get() bool

fn (Context) is_json #

fn (ctx &Context) is_json() bool

fn (Context) is_post #

fn (ctx &Context) is_post() bool

fn (Context) is_put #

fn (ctx &Context) is_put() bool

fn (Context) json #

fn (ctx &Context) json[T]() !T

fn (Context) json_response #

fn (mut ctx Context) json_response[T](status_code int, data T) Response

fn (Context) method #

fn (ctx &Context) method() string

fn (Context) no_content #

fn (mut ctx Context) no_content() Response

fn (Context) not_found #

fn (mut ctx Context) not_found(message string) Response

fn (Context) ok #

fn (mut ctx Context) ok(body string) Response

fn (Context) param #

fn (ctx &Context) param(key string) string

fn (Context) parse_multipart_form #

fn (mut ctx Context) parse_multipart_form(config UploadConfig) !MultipartForm

parse_multipart_form parses multipart form data from request

fn (Context) path #

fn (ctx &Context) path() string

fn (Context) query #

fn (ctx &Context) query(key string) string

Request convenience methods

fn (Context) query_all #

fn (ctx &Context) query_all(key string) []string

fn (Context) redirect #

fn (mut ctx Context) redirect(url string, status_code int) Response

fn (Context) redirect_permanent #

fn (mut ctx Context) redirect_permanent(url string) Response

fn (Context) redirect_temporary #

fn (mut ctx Context) redirect_temporary(url string) Response

fn (Context) regenerate_session #

fn (mut ctx Context) regenerate_session() !

fn (Context) render #

fn (mut ctx Context) render(template_path string, data map[string]string) Response

render renders a template file with layout and returns HTML response

fn (Context) render_data #

fn (mut ctx Context) render_data(template_path string, data map[string]vm.Any) Response

render_data renders a template with structured data (arrays, objects, etc.) This method accepts map[string]vm.Any to support passing complex data to templates

fn (Context) render_string #

fn (mut ctx Context) render_string(template string, data map[string]string) Response

render_string renders a template string and returns HTML response

fn (Context) render_with_status #

fn (mut ctx Context) render_with_status(status_code int, template_path string, data map[string]string) Response

render_with_status renders a template with custom status code

fn (Context) session_clear #

fn (mut ctx Context) session_clear()

fn (Context) session_delete #

fn (mut ctx Context) session_delete(key string)

fn (Context) session_get #

fn (ctx &Context) session_get(key string) ?string

fn (Context) session_set #

fn (mut ctx Context) session_set(key string, value string)

fn (Context) set #

fn (mut ctx Context) set(key string, value string)

Middleware data storage

fn (Context) set_user #

fn (mut ctx Context) set_user(user_id int)

fn (Context) status #

fn (mut ctx Context) status(code int) Response

Response convenience methods

fn (Context) text #

fn (mut ctx Context) text(status_code int, body string) Response

fn (Context) unauthorized #

fn (mut ctx Context) unauthorized(message string) Response

fn (Context) url_for #

fn (ctx &Context) url_for(path string, query_params ...map[string]string) string

url_for generates a URL for a given path with optional query parameters

Examples

ctx.url_for('/users/123') -> '/users/123'
ctx.url_for('/search', {'q': 'varel', 'page': '2'}) -> '/search?q=varel&page=2'

fn (Context) user_id #

fn (ctx &Context) user_id() ?int

fn (Cookie) make_http_only #

fn (c Cookie) make_http_only() Cookie

fn (Cookie) make_secure #

fn (c Cookie) make_secure() Cookie

fn (Cookie) with_domain #

fn (c Cookie) with_domain(d string) Cookie

fn (Cookie) with_expires #

fn (c Cookie) with_expires(expires string) Cookie

fn (Cookie) with_max_age #

fn (c Cookie) with_max_age(seconds int) Cookie

fn (Cookie) with_path #

fn (c Cookie) with_path(p string) Cookie

fn (Cookie) with_same_site #

fn (c Cookie) with_same_site(same_site string) Cookie

struct FileValidator #

struct FileValidator {
pub:
	max_size      i64
	allowed_types []string
	allowed_exts  []string
}

Validate file upload

fn (FileValidator) validate #

fn (v &FileValidator) validate(file &UploadedFile) !

validate checks if an uploaded file meets requirements

struct FormFile #

struct FormFile {
pub:
	name         string
	filename     string
	content_type string
	data         []u8
}

FormFile represents an uploaded file

struct MultipartForm #

struct MultipartForm {
pub mut:
	fields map[string][]string       // Regular form fields (name -> values)
	files  map[string][]UploadedFile // File uploads (field name -> files)
}

MultipartForm contains parsed multipart/form-data

struct Request #

struct Request {
pub mut:
	raw     &vhttp.Request // Access to underlying V request
	method  string
	path    string
	query   urllib.Values // Leverage V's Values type for query params
	headers vhttp.Header
	body    string
	cookies map[string]string
	// Lazy-loaded caches
	form_parsed      bool
	form_values      map[string][]string
	multipart_parsed bool
	multipart_files  map[string][]FormFile
}

Request wraps V's http.Request with server-focused API

fn (Request) query_param #

fn (req &Request) query_param(key string) string

Query parameter access (leveraging urllib.Values)

fn (Request) query_params #

fn (req &Request) query_params(key string) []string

fn (Request) has_query #

fn (req &Request) has_query(key string) bool

fn (Request) parse_form #

fn (mut req Request) parse_form() !

Form parsing (urlencoded) with multi-value support

fn (Request) form_value #

fn (mut req Request) form_value(key string) string

fn (Request) form_values #

fn (mut req Request) form_values(key string) []string

fn (Request) parse_multipart_form #

fn (mut req Request) parse_multipart_form() !

Multipart form parsing (leverage V's parser)

fn (Request) file #

fn (mut req Request) file(key string) ?FormFile

fn (Request) json #

fn (req &Request) json[T]() !T

JSON parsing

fn (Request) is_json #

fn (req &Request) is_json() bool

fn (Request) header #

fn (req &Request) header(key string) string

Header helpers

fn (Request) content_type #

fn (req &Request) content_type() string

fn (Request) user_agent #

fn (req &Request) user_agent() string

fn (Request) accept #

fn (req &Request) accept() string

fn (Request) cookie #

fn (req &Request) cookie(name string) string

Cookie access

fn (Request) is_get #

fn (req &Request) is_get() bool

Request type checks

fn (Request) is_post #

fn (req &Request) is_post() bool

fn (Request) is_put #

fn (req &Request) is_put() bool

fn (Request) is_delete #

fn (req &Request) is_delete() bool

fn (Request) is_patch #

fn (req &Request) is_patch() bool

struct Response #

struct Response {
pub mut:
	status_code int = 200
	headers     vhttp.Header
	body        string
	cookies     []Cookie
}

Response builder with chainable API

fn (Response) status #

fn (mut resp Response) status(code int) Response

Status setters (chainable)

fn (Response) ok #

fn (mut resp Response) ok() Response

fn (Response) created #

fn (mut resp Response) created() Response

fn (Response) accepted #

fn (mut resp Response) accepted() Response

fn (Response) no_content #

fn (mut resp Response) no_content() Response

fn (Response) moved_permanently #

fn (mut resp Response) moved_permanently() Response

fn (Response) found #

fn (mut resp Response) found() Response

fn (Response) see_other #

fn (mut resp Response) see_other() Response

fn (Response) temporary_redirect #

fn (mut resp Response) temporary_redirect() Response

fn (Response) permanent_redirect #

fn (mut resp Response) permanent_redirect() Response

fn (Response) bad_request #

fn (mut resp Response) bad_request() Response

fn (Response) unauthorized #

fn (mut resp Response) unauthorized() Response

fn (Response) forbidden #

fn (mut resp Response) forbidden() Response

fn (Response) not_found #

fn (mut resp Response) not_found() Response

fn (Response) method_not_allowed #

fn (mut resp Response) method_not_allowed() Response

fn (Response) conflict #

fn (mut resp Response) conflict() Response

fn (Response) unprocessable_entity #

fn (mut resp Response) unprocessable_entity() Response

fn (Response) internal_error #

fn (mut resp Response) internal_error() Response

fn (Response) not_implemented #

fn (mut resp Response) not_implemented() Response

fn (Response) bad_gateway #

fn (mut resp Response) bad_gateway() Response

fn (Response) service_unavailable #

fn (mut resp Response) service_unavailable() Response

fn (Response) set_body #

fn (mut resp Response) set_body(body string) Response

Body setters (chainable)

fn (Response) text #

fn (mut resp Response) text(body string) Response

fn (Response) html #

fn (mut resp Response) html(body string) Response

fn (Response) json #

fn (mut resp Response) json[T](data T) Response

fn (Response) xml #

fn (mut resp Response) xml(body string) Response

fn (Response) header #

fn (mut resp Response) header(key string, value string) Response

Header setters (chainable)

fn (Response) content_type #

fn (mut resp Response) content_type(ct string) Response

fn (Response) location #

fn (mut resp Response) location(url string) Response

fn (Response) cookie #

fn (mut resp Response) cookie(cookie Cookie) Response

Cookie setter

fn (Response) to_vhttp #

fn (resp &Response) to_vhttp() Response

Convert to V's Response

struct UploadConfig #

struct UploadConfig {
pub:
	max_file_size   i64 = 10 * 1024 * 1024 // 10MB default
	max_memory      i64 = 5 * 1024 * 1024  // 5MB in memory, rest on disk
	allowed_types   []string // Empty = allow all
	temp_dir        string = '/tmp' // Temporary upload directory
	keep_extensions bool   = true   // Preserve original file extensions
}

UploadConfig configures file upload behavior

struct UploadedFile #

struct UploadedFile {
pub:
	field_name   string            // Form field name
	filename     string            // Original filename from client
	content_type string            // MIME type
	size         i64               // File size in bytes
	headers      map[string]string // Part headers
pub mut:
	temp_path string // Temporary file path on disk
}

UploadedFile represents a file uploaded via multipart/form-data