Pharos 0.7.23
Modern Web Framework & Web App Hosting Service.
Loading...
Searching...
No Matches
native_dynamic_multipart.c File Reference

Pharos-owned multipart form-data parsing for in-process dynamic handling. More...

#include "native_dynamic_multipart.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Functions

static const unsigned char * multipart_memmem (const unsigned char *haystack, size_t haystack_len, const unsigned char *needle, size_t needle_len)
 Finds a byte pattern within a buffer.
static char * multipart_dup_range (const char *start, size_t len)
 Allocates a NUL-terminated copy of a byte range.
char * native_dynamic_multipart_boundary (const char *content_type)
 Extracts the MIME boundary from a Content-Type header value.
static char * multipart_header_param_dup (const char *headers_text, const char *key)
 Extracts a quoted parameter value from MIME part headers.
int native_dynamic_multipart_find_part (const unsigned char *body, size_t body_len, const char *content_type, const char *field_name, char **out_text, char **out_filename, const unsigned char **out_content_start, size_t *out_content_len)
 Finds a named part in a multipart request body.
char * native_dynamic_multipart_filename (const unsigned char *body, size_t body_len, const char *content_type, const char *field_name)
 Extracts the uploaded filename for a named multipart file part.
char * native_dynamic_multipart_extract_text (const unsigned char *body, size_t body_len, const char *content_type, const char *field_name)
 Extracts a text value from a multipart form field.
int native_dynamic_multipart_extract_file (const unsigned char *body, size_t body_len, const char *content_type, const char *field_name, const char *output_path)
 Extracts file content from a multipart file part and writes it to disk.

Detailed Description

Pharos-owned multipart form-data parsing for in-process dynamic handling.

Provides boundary extraction, part lookup, filename extraction, and file-part extraction so that the dynamic runtime can process multipart uploads without delegating to a child process or relying on app-owned shell logic.

This module is the canonical Pharos-owned implementation of multipart parsing. App-owned shell code must not replicate this logic.

Definition in file native_dynamic_multipart.c.

Function Documentation

◆ multipart_dup_range()

char * multipart_dup_range ( const char * start,
size_t len )
static

Allocates a NUL-terminated copy of a byte range.

Parameters
startStart of the range (need not be NUL-terminated).
lenNumber of bytes to copy.
Returns
Newly allocated NUL-terminated string, or NULL on allocation failure.

Definition at line 59 of file native_dynamic_multipart.c.

References len.

Referenced by multipart_header_param_dup(), native_dynamic_multipart_boundary(), and native_dynamic_multipart_find_part().

◆ multipart_header_param_dup()

char * multipart_header_param_dup ( const char * headers_text,
const char * key )
static

Extracts a quoted parameter value from MIME part headers.

Parameters
headers_textNUL-terminated headers block for one part.
keyParameter name (e.g. name or filename).
Returns
Newly allocated parameter value, or NULL when absent.

Definition at line 115 of file native_dynamic_multipart.c.

References multipart_dup_range().

Referenced by native_dynamic_multipart_find_part().

◆ multipart_memmem()

const unsigned char * multipart_memmem ( const unsigned char * haystack,
size_t haystack_len,
const unsigned char * needle,
size_t needle_len )
static

Finds a byte pattern within a buffer.

Parameters
haystackBuffer to search.
haystack_lenByte length of haystack.
needlePattern to locate.
needle_lenByte length of needle.
Returns
Pointer to first occurrence within haystack, or NULL.

Definition at line 34 of file native_dynamic_multipart.c.

Referenced by native_dynamic_multipart_find_part().

◆ native_dynamic_multipart_boundary()

char * native_dynamic_multipart_boundary ( const char * content_type)

Extracts the MIME boundary from a Content-Type header value.

Extracts the MIME boundary string from a Content-Type header.

Handles both quoted and unquoted boundary parameters.

Parameters
content_typeThe full Content-Type header value.
Returns
Newly allocated boundary string (without the leading -- ), or NULL when the boundary cannot be determined.

Extracts the MIME boundary from a Content-Type header value.

Parameters
content_typeThe full Content-Type header value.
Returns
Newly allocated boundary string (without the leading -- ), or NULL when the boundary cannot be determined. Caller must free.

Definition at line 82 of file native_dynamic_multipart.c.

References multipart_dup_range().

Referenced by native_dynamic_multipart_find_part().

◆ native_dynamic_multipart_extract_file()

int native_dynamic_multipart_extract_file ( const unsigned char * body,
size_t body_len,
const char * content_type,
const char * field_name,
const char * output_path )

Extracts file content from a multipart file part and writes it to disk.

Parameters
bodyRaw multipart body bytes.
body_lenByte length of body.
content_typeThe full Content-Type header.
field_nameThe form field name of the file input.
output_pathDestination file path on disk.
Returns
0 on success, non-zero on failure.

Definition at line 306 of file native_dynamic_multipart.c.

References native_dynamic_multipart_find_part().

Referenced by handle_dynamic_multipart_extract_file_command().

◆ native_dynamic_multipart_extract_text()

char * native_dynamic_multipart_extract_text ( const unsigned char * body,
size_t body_len,
const char * content_type,
const char * field_name )

Extracts a text value from a multipart form field.

Parameters
bodyRaw multipart body bytes.
body_lenByte length of body.
content_typeThe full Content-Type header.
field_nameThe form field name to extract.
Returns
Newly allocated text value, or NULL when not found. Caller must free.

Definition at line 295 of file native_dynamic_multipart.c.

References native_dynamic_multipart_find_part().

Referenced by handle_dynamic_multipart_extract_text_command(), request_form_value_dup_logic_local(), and request_form_value_dup_runtime().

◆ native_dynamic_multipart_filename()

char * native_dynamic_multipart_filename ( const unsigned char * body,
size_t body_len,
const char * content_type,
const char * field_name )

Extracts the uploaded filename for a named multipart file part.

Parameters
bodyRaw multipart body bytes.
body_lenByte length of body.
content_typeThe full Content-Type header.
field_nameThe form field name of the file input.
Returns
Newly allocated filename string, or NULL when not found. Caller must free.

Definition at line 284 of file native_dynamic_multipart.c.

References native_dynamic_multipart_find_part().

Referenced by handle_dynamic_multipart_filename_command().

◆ native_dynamic_multipart_find_part()

int native_dynamic_multipart_find_part ( const unsigned char * body,
size_t body_len,
const char * content_type,
const char * field_name,
char ** out_text,
char ** out_filename,
const unsigned char ** out_content_start,
size_t * out_content_len )

Finds a named part in a multipart request body.

Searches body for a part whose Content-Disposition name matches field_name. When found, optionally returns the text content, the uploaded filename, and/or the raw content range within body.

Parameters
bodyRaw multipart body bytes.
body_lenByte length of body.
content_typeThe full Content-Type header (must contain the boundary).
field_nameThe form field name to locate.
out_textReceives a NUL-terminated copy of the part content (may be NULL to skip).
out_filenameReceives the uploaded filename (may be NULL to skip).
out_content_startReceives a pointer into body at the content start (may be NULL to skip).
out_content_lenReceives the content byte length (may be NULL to skip).
Returns
1 when a matching part was found, 0 otherwise.

Definition at line 140 of file native_dynamic_multipart.c.

References multipart_dup_range(), multipart_header_param_dup(), multipart_memmem(), and native_dynamic_multipart_boundary().

Referenced by native_dynamic_multipart_extract_file(), native_dynamic_multipart_extract_text(), native_dynamic_multipart_filename(), and store_uploaded_media().