httpd_util(3)

πŸ“¦
version 29.0.6 Β· 3 versions
, in ubuntu stonking

πŸ’– httpd_util β€” Erlang Module Definition πŸ’–

okay so bestie, welcome to httpd_util, a little module living in the Erlang Module Definition section of the manual pages (inets, that's the manual family, iykyk). It's the grab-bag of helper functions you reach for when you're building Erlang web server API modules. Let's get into it πŸ’…


✨ NAME ✨

httpd_util β€” miscellaneous utility functions to be used when implementing Erlang web server API modules.


πŸ¦‹ DESCRIPTION πŸ¦‹

This module hands the Erlang web server API module programmer a whole pile of miscellaneous utility functions. Think of it as the friend who always has a spare charger, a snack, and a regex. 🫢

NOTE: bestie, listen. This module is only recommended for using with httpd β€” for other cases it should be considered as deprecated. Not "old", not "vintage", deprecated. Use it with httpd or don't use it at all, periodt. πŸ’€

See also

httpd


🌸 FUNCTIONS 🌸

convert_request_date(DateString) -> ErlDate | bad_date

convert_request_date(DateString) -> ErlDate | bad_date
    when
        DateString :: string(),
        ErlDate :: calendar:datetime().

convert_request_date/1 converts DateString into the Erlang date format. And the vibe check here? DateString must be in one of the three date formats defined in RFC 2616. No fourth format, no improvisation, no "I made the date cute". πŸ“…


create_etag(FileInfo) -> Etag

create_etag(FileInfo) -> Etag
    when
        FileInfo :: file:file_info(),
        Etag :: string().

create_etag/1 calculates the Etag for a file from its size and its time for last modification. FileInfo is a record defined in kernel/include/file.hrl β€” yes, that exact path, she's not guessing. πŸ”₯


day(NthDayOfWeek) -> DayOfWeek

day(NthDayOfWeek) -> DayOfWeek
    when
        NthDayOfWeek :: 1..7,
        DayOfWeek :: string().

day/1 converts the day of the week (NthDayOfWeek) from an integer (1-7) to an abbreviated string, that is:

  • 1 = "Mon"
  • 2 = "Tue"
  • ...
  • 7 = "Sat"

(no, there's no abbreviated "Sun" in this list, that's just how it is bestie πŸ™ˆ)


lookup(EtsTable, Key, Undefined) -> Result

lookup(EtsTable, Key, Undefined) -> Result
    when
        EtsTable :: ets:table(),
        Key :: term(),
        Undefined :: term(),
        Result :: term() | Undefined.

lookup extracts {Key, Value} tuples from ETSTable and returns the Value associated with Key. If ETSTable is of type bag, only the first Value associated with Key is returned. And if no Value is found? lookup/2 returns undefined, while lookup/3 returns Undefined (the thing you handed her). So basically: two args, you get undefined; three args, you get your Undefined. πŸ’œ


lookup_mime(ConfigDB, Suffix, Undefined) -> MimeType

lookup_mime(ConfigDB, Suffix, Undefined) -> MimeType
    when
        ConfigDB :: ets:tid(),
        Suffix :: string(),
        Undefined :: term(),
        MimeType :: string() | Undefined.

lookup_mime returns the MIME type associated with a specific file suffix, as specified in the file mime.types β€” which lives in the config directory, in case you were about to go looking in the wrong folder. 🧚


lookup_mime_default(ConfigDB, Suffix, Undefined) -> MimeType

lookup_mime_default(ConfigDB, Suffix, Undefined) -> MimeType
    when
        ConfigDB :: ets:tid(),
        Suffix :: string(),
        Undefined :: term(),
        MimeType :: string() | Undefined.

lookup_mime_default returns the MIME type associated with a specific file suffix as specified in the mime.types file (located in the config directory, same as her sister above). But here's the slay: if no appropriate association is found, the value of DefaultType is returned. Built-in fallback energy. πŸ‘‘


message(StatusCode, PhraseArgs, ConfigDB) -> Message

message(StatusCode, PhraseArgs, ConfigDB) -> Message
    when
        StatusCode ::
            301 | 304 | 400 | 401 | 403 | 404 | 408 | 412 |
            413 | 414 | 500 | 501 | 503 | 504,
        PhraseArgs :: term(),
        ConfigDB :: ets:tid(),
        Message :: string().

message/3 returns an informative HTTP 1.1 status string in HTML. Every single StatusCode requires its own specific PhraseArgs, so let's go down the list, no skipping bestie:

  • 301
    • string/0: A URL pointing at the new document position.
  • 400 | 401 | 500
    • none (no PhraseArgs).
  • 403 | 404
    • string/0: A Request-URI as described in RFC 2616.
  • 501
    • {Method, RequestURI, HTTPVersion}: The HTTP Method, Request-URI, and HTTP-Version as defined in RFC 2616.
  • 504
    • string/0: A string describing why the service was unavailable.

month(NthMonth) -> Month

month(NthMonth) -> Month
    when
        NthMonth :: 1..12,
        Month :: string().

month/1 converts the month NthMonth as an integer (1-12) to an abbreviated string, that is:

  • 1 = "Jan"
  • 2 = "Feb"
  • ...
  • 12 = "Dec"

twelve months, twelve strings, consistent queen. πŸŽ€


multi_lookup(EtsTable, Key) -> Result

multi_lookup(EtsTable, Key) -> Result
    when
        EtsTable :: ets:tid(),
        Key :: term(),
        Result :: list() | [term()].

multi_lookup extracts all {Key, Value} tuples from an ETSTable and returns all Values associated with Key in a list. Where lookup gave you one, multi_lookup gives you the whole group chat. 🌈


reason_phrase(StatusCode) -> Description

reason_phrase(StatusCode) -> Description
    when
        StatusCode ::
            100 | 101 | 102 | 200 | 201 | 202 | 203 |
            204 | 205 | 206 | 207 | 226 | 300 | 301 |
            302 | 303 | 304 | 305 | 306 | 307 | 308 |
            400 | 401 | 402 | 403 | 404 | 405 | 406 |
            407 | 408 | 409 | 410 | 411 | 412 | 413 |
            414 | 415 | 416 | 417 | 422 | 423 | 424 |
            425 | 426 | 500 | 501 | 502 | 503 | 504 |
            505 | 507,
        Description :: string().

reason_phrase returns the Description of an HTTP 1.1 StatusCode β€” for example, 200 is "OK" and 201 is "Created". For more information, see RFC 2616. πŸ“œ


rfc1123_date(LocalTime) -> RFC1123Date

rfc1123_date(LocalTime) -> RFC1123Date
    when
        LocalTime :: calendar:datetime() | undefined,
        RFC1123Date :: string() | undefined.

rfc1123_date/0 returns the current date in RFC 1123 format. And rfc_date/1 converts the date in the Erlang format to the RFC 1123 date format. Two arities, two jobs, both iconic. ⭐


split(String, RegExp, N) -> SplitRes

split(String, RegExp, N) -> SplitRes
    when
        String :: string(),
        RegExp :: string(),
        N :: non_neg_integer(),
        SplitRes :: {ok, FieldList} | {error, term()},
        FieldList :: [string()].

split/3 splits String in N chunks using RegExp. And the tea: split/3 is equivalent to re:split/3, with the exception that N defines the maximum number of fields in FieldList. That's the whole difference, don't let anyone tell you otherwise. πŸ’…


split_path(URIString) -> {Path, QueryStringOrPathInfo}

split_path(URIString) -> {Path, QueryStringOrPathInfo}
    when
        URIString :: string(),
        Path :: string(),
        QueryStringOrPathInfo :: string().

split_path/1 splits RequestLine into a file reference (Path) and a QueryString or a PathInfo string as specified in RFC 2616. A QueryString is isolated from Path with a question mark (?), and PathInfo with a slash (/). In the case of a QueryString, everything before ? is a Path and everything after ? is a QueryString. In the case of a PathInfo, RequestLine is scanned from left-to-right on the hunt for the longest possible Path being a file or a directory. Everything after the longest possible Path, isolated with a /, is regarded as PathInfo. She's thorough, she's directional, she's taking her time. πŸ”


split_script_path(URIString) -> Split

split_script_path(URIString) -> Split
    when
        URIString :: string(),
        Split ::
            not_a_script |
            {Path, {PathInfo, QueryString}} |
            {Path, []},
        Path :: string(),
        QueryString :: string(),
        PathInfo :: string().

split_script_path/1 is equivalent to split_path/1 with one exception. If the longest possible path is not a regular, accessible, and executable file, then not_a_script is returned. Three checks failed, one honest answer. No cap. 😭


Ericsson AB β€” that's the source, the whole inets 9.7.2 situation, and this is httpd_util(3), bestie. Go forth and build your web server API modules, and remember: httpd only, or it's deprecated territory. mwah πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ«Ά

πŸ“¦ where this page comes from