Struct HttpMessage

Source
pub struct HttpMessage<'lua> { /* private fields */ }
Expand description

This class contains all functions to manipulate an HTTP message. For now, this class is only available from a filter context.

Implementations§

Source§

impl<'lua> HttpMessage<'lua>

Source

pub fn add_header(&self, name: &str, value: impl AsRef<[u8]>) -> Result<()>

Appends an HTTP header field in the HTTP message whose name is specified in name and value is defined in value.

Source

pub fn append(&self, data: impl AsRef<[u8]>) -> Result<isize>

Copies the string at the end of incoming data of the HTTP message. The function returns the copied length on success or -1 if data cannot be copied.

Source

pub fn body( &self, offset: Option<isize>, length: Option<isize>, ) -> Result<Option<LuaString<'lua>>>

Returns length bytes of incoming data from the HTTP message, starting at the offset. The data are not removed from the buffer.

Source

pub fn channel(&self) -> Result<Channel<'lua>>

Returns a corresponding channel attached to the HTTP message.

Source

pub fn eom(&self) -> Result<bool>

Returns true if the end of message is reached.

Source

pub fn del_header(&self, name: &str) -> Result<()>

Removes all HTTP header fields in the HTTP message whose name is specified in name.

Source

pub fn get_headers(&self) -> Result<Headers<'lua>>

Returns a table containing all the headers of the HTTP message.

Source

pub fn get_stline(&self) -> Result<Table<'lua>>

Returns a table containing the start-line of the HTTP message.

Source

pub fn forward(&self, length: usize) -> Result<usize>

Forwards length bytes of data from the HTTP message. Returns the amount of data forwarded.

Because it is called in the filter context, it never yield. Only available incoming data may be forwarded, event if the requested length exceeds the available amount of incoming data.

Source

pub fn input(&self) -> Result<usize>

Returns the length of incoming data in the HTTP message from the calling filter point of view.

Source

pub fn insert( &self, data: impl AsRef<[u8]>, offset: Option<isize>, ) -> Result<isize>

Copies the data at the offset in incoming data of the HTTP message. Returns the copied length on success or -1 if data cannot be copied.

By default, if no offset is provided, the string is copied in front of incoming data. A positive offset is relative to the beginning of incoming data of the channel buffer while negative offset is relative to their end.

Source

pub fn is_full(&self) -> Result<bool>

Returns true if the HTTP message is full.

Source

pub fn is_resp(&self) -> Result<bool>

Returns true if the HTTP message is the response one.

Source

pub fn may_recv(&self) -> Result<bool>

Returns true if the HTTP message may still receive data.

Source

pub fn output(&self) -> Result<usize>

Returns the length of outgoing data of the HTTP message.

Source

pub fn prepend(&self, data: impl AsRef<[u8]>) -> Result<isize>

Copies the data in front of incoming data of the HTTP message. Returns the copied length on success or -1 if data cannot be copied.

Source

pub fn remove( &self, offset: Option<isize>, length: Option<usize>, ) -> Result<isize>

Removes length bytes of incoming data of the HTTP message, starting at offset. Returns number of bytes removed on success.

Source

pub fn rep_header(&self, name: &str, regex: &str, replace: &str) -> Result<()>

Matches the regular expression in all occurrences of header field name according to regex, and replaces them with the replace.

The replacement value can contain back references like 1, 2, … This function acts on whole header lines, regardless of the number of values they may contain.

Source

pub fn rep_value(&self, name: &str, regex: &str, replace: &str) -> Result<()>

Matches the regular expression on every comma-delimited value of header field name according to regex, and replaces them with the replace.

The replacement value can contain back references like 1, 2, …

Source

pub fn send(&self, data: impl AsRef<[u8]>) -> Result<isize>

Requires immediate send of the data. It means the data is copied at the beginning of incoming data of the HTTP message and immediately forwarded.

Because it is called in the filter context, it never yield.

Source

pub fn set( &self, data: impl AsRef<[u8]>, offset: Option<isize>, length: Option<usize>, ) -> Result<isize>

Replaces length bytes of incoming data of the HTTP message, starting at offset, by the string data. Returns the copied length on success or -1 if data cannot be copied.

Source

pub fn set_eom(&self, eom: bool) -> Result<()>

Sets or removes the flag that indicates end of message.

Source

pub fn set_header(&self, name: &str, value: impl AsRef<[u8]>) -> Result<()>

Replaces all occurrence of all header matching the name, by only one containing the value.

Source

pub fn set_method(&self, method: &str) -> Result<()>

Rewrites the request method.

Source

pub fn set_path(&self, path: &str) -> Result<()>

Rewrites the request path.

Source

pub fn set_query(&self, query: &str) -> Result<()>

Rewrites the request’s query string which appears after the first question mark “?”.

Source

pub fn set_status(&self, status: u16, reason: Option<&str>) -> Result<()>

Rewrites the response status code with the new status and optional reason. If no custom reason is provided, it will be generated from the status.

Source

pub fn set_uri(&self, uri: &str) -> Result<()>

Rewrites the request URI.

Methods from Deref<Target = Table<'lua>>§

Source

pub fn set<K, V>(&self, key: K, value: V) -> Result<(), Error>
where K: IntoLua<'lua>, V: IntoLua<'lua>,

Sets a key-value pair in the table.

If the value is nil, this will effectively remove the pair.

This might invoke the __newindex metamethod. Use the raw_set method if that is not desired.

§Examples

Export a value as a global to make it usable from Lua:

let globals = lua.globals();

globals.set("assertions", cfg!(debug_assertions))?;

lua.load(r#"
    if assertions == true then
        -- ...
    elseif assertions == false then
        -- ...
    else
        error("assertions neither on nor off?")
    end
"#).exec()?;
Source

pub fn get<K, V>(&self, key: K) -> Result<V, Error>
where K: IntoLua<'lua>, V: FromLua<'lua>,

Gets the value associated to key from the table.

If no value is associated to key, returns the nil value.

This might invoke the __index metamethod. Use the raw_get method if that is not desired.

§Examples

Query the version of the Lua interpreter:

let globals = lua.globals();

let version: String = globals.get("_VERSION")?;
println!("Lua version: {}", version);
Source

pub fn contains_key<K>(&self, key: K) -> Result<bool, Error>
where K: IntoLua<'lua>,

Checks whether the table contains a non-nil value for key.

This might invoke the __index metamethod.

Source

pub fn push<V>(&self, value: V) -> Result<(), Error>
where V: IntoLua<'lua>,

Appends a value to the back of the table.

This might invoke the __len and __newindex metamethods.

Source

pub fn pop<V>(&self) -> Result<V, Error>
where V: FromLua<'lua>,

Removes the last element from the table and returns it.

This might invoke the __len and __newindex metamethods.

Source

pub fn equals<T>(&self, other: T) -> Result<bool, Error>
where T: AsRef<Table<'lua>>,

Compares two tables for equality.

Tables are compared by reference first. If they are not primitively equals, then mlua will try to invoke the __eq metamethod. mlua will check self first for the metamethod, then other if not found.

§Examples

Compare two tables using __eq metamethod:

let table1 = lua.create_table()?;
table1.set(1, "value")?;

let table2 = lua.create_table()?;
table2.set(2, "value")?;

let always_equals_mt = lua.create_table()?;
always_equals_mt.set("__eq", lua.create_function(|_, (_t1, _t2): (Table, Table)| Ok(true))?)?;
table2.set_metatable(Some(always_equals_mt));

assert!(table1.equals(&table1.clone())?);
assert!(table1.equals(&table2)?);
Source

pub fn raw_set<K, V>(&self, key: K, value: V) -> Result<(), Error>
where K: IntoLua<'lua>, V: IntoLua<'lua>,

Sets a key-value pair without invoking metamethods.

Source

pub fn raw_get<K, V>(&self, key: K) -> Result<V, Error>
where K: IntoLua<'lua>, V: FromLua<'lua>,

Gets the value associated to key without invoking metamethods.

Source

pub fn raw_insert<V>(&self, idx: i64, value: V) -> Result<(), Error>
where V: IntoLua<'lua>,

Inserts element value at position idx to the table, shifting up the elements from table[idx]. The worst case complexity is O(n), where n is the table length.

Source

pub fn raw_push<V>(&self, value: V) -> Result<(), Error>
where V: IntoLua<'lua>,

Appends a value to the back of the table without invoking metamethods.

Source

pub fn raw_pop<V>(&self) -> Result<V, Error>
where V: FromLua<'lua>,

Removes the last element from the table and returns it, without invoking metamethods.

Source

pub fn raw_remove<K>(&self, key: K) -> Result<(), Error>
where K: IntoLua<'lua>,

Removes a key from the table.

If key is an integer, mlua shifts down the elements from table[key+1], and erases element table[key]. The complexity is O(n) in the worst case, where n is the table length.

For other key types this is equivalent to setting table[key] = nil.

Source

pub fn clear(&self) -> Result<(), Error>

Clears the table, removing all keys and values from array and hash parts, without invoking metamethods.

This method is useful to clear the table while keeping its capacity.

Source

pub fn len(&self) -> Result<i64, Error>

Returns the result of the Lua # operator.

This might invoke the __len metamethod. Use the raw_len method if that is not desired.

Source

pub fn raw_len(&self) -> usize

Returns the result of the Lua # operator, without invoking the __len metamethod.

Source

pub fn is_empty(&self) -> bool

Returns true if the table is empty, without invoking metamethods.

It checks both the array part and the hash part.

Source

pub fn get_metatable(&self) -> Option<Table<'lua>>

Returns a reference to the metatable of this table, or None if no metatable is set.

Unlike the getmetatable Lua function, this method ignores the __metatable field.

Source

pub fn set_metatable(&self, metatable: Option<Table<'lua>>)

Sets or removes the metatable of this table.

If metatable is None, the metatable is removed (if no metatable is set, this does nothing).

Source

pub fn to_pointer(&self) -> *const c_void

Converts this table to a generic C pointer.

Different tables will give different pointers. There is no way to convert the pointer back to its original value.

Typically this function is used only for hashing and debug information.

Source

pub fn for_each<K, V>( &self, f: impl FnMut(K, V) -> Result<(), Error>, ) -> Result<(), Error>
where K: FromLua<'lua>, V: FromLua<'lua>,

Iterates over the pairs of the table, invoking the given closure on each pair.

This method is similar to Table::pairs, but optimized for performance. It does not invoke the __pairs metamethod.

Trait Implementations§

Source§

impl<'lua> Clone for HttpMessage<'lua>

Source§

fn clone(&self) -> HttpMessage<'lua>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'lua> Deref for HttpMessage<'lua>

Source§

type Target = Table<'lua>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'lua> FromLua<'lua> for HttpMessage<'lua>

Source§

fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result<Self>

Performs the conversion.

Auto Trait Implementations§

§

impl<'lua> Freeze for HttpMessage<'lua>

§

impl<'lua> !RefUnwindSafe for HttpMessage<'lua>

§

impl<'lua> !Send for HttpMessage<'lua>

§

impl<'lua> !Sync for HttpMessage<'lua>

§

impl<'lua> Unpin for HttpMessage<'lua>

§

impl<'lua> !UnwindSafe for HttpMessage<'lua>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<'lua, T> FromLuaMulti<'lua> for T
where T: FromLua<'lua>,

Source§

fn from_lua_multi(values: MultiValue<'lua>, lua: &'lua Lua) -> Result<T, Error>

Performs the conversion. Read more
Source§

fn from_lua_args( args: MultiValue<'lua>, i: usize, to: Option<&str>, lua: &'lua Lua, ) -> Result<T, Error>

Source§

unsafe fn from_stack_multi(nvals: i32, lua: &'lua Lua) -> Result<T, Error>

Source§

unsafe fn from_stack_args( nargs: i32, i: usize, to: Option<&str>, lua: &'lua Lua, ) -> Result<T, Error>

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.