libapa...nection(3pm)
π NAME π
Apache2::Connection β bestie, it's the Perl API for the Apache connection object π«Ά
(also heads up, this is the libapa...nection(3pm) page, from User Contributed Perl Documentation β it's long so we're doing it in parts π
)
β¨ Synopsis β¨
okay so here's the whole vibe in one giant code block, no cap:
use Apache2::Connection ();
use Apache2::RequestRec ();
my $c = $r->connection;
my $c = $r->connection;
# is connection still open?
$status = $c->aborted;
# base server
$base_server = $c->base_server();
# needed for creating buckets/brigades
$ba = $c->bucket_alloc();
# client's socket
$socket = $c->client_socket;
# unique connection id
$id = $c->id();
# connection filters stack
$input_filters = $c->input_filters();
$output_filters = $c->output_filters();
# keep the connection alive?
$status = $c->keepalive();
# how many requests served over the current connection
$served = $c->keepalives();
# this connection's local and remote socket addresses
$local_sa = $c->local_addr();
$remote_sa = $c->remote_addr();
# local and remote hostnames
$local_host = $c->local_host();
$remote_host = $c->get_remote_host();
$remote_host = $c->remote_host();
# server and remote client's IP addresses
$local_ip = $c->local_ip();
$remote_ip = $c->remote_ip();
# connection level Apache notes
$notes = $c->notes();
# this connection's pool
$p = $c->pool();
like. that's the whole tour. iconic. π
πΈ Description πΈ
so Apache2::RequestRec is the one that hands you the Perl API for the Apache connection record object. that's the whole tea for now β it's giving "short but essential", and honestly? obsessed π
part 1 done bestie, don't go anywhere, more is coming π
β¨ API β¨
okay bestie, this is the part you're actually here for π Apache2::Connection hands you these functions and/or methods:
πΈ aborted πΈ
Checking if the connection is still alive or if it already ghosted you π»
$status = $c->aborted();
- obj:
$c(Apache2::Connection object) - ret:
$status( boolean ) β true if the connection got aborted, false if it's still open - since:
2.0.00
πΈ base_server πΈ
The physical server this connection landed on β like, main server or the vhost π
$base_server = $c->base_server();
- obj:
$c(Apache2::Connection object) - ret:
$base_server(Apache2::Server object) - since:
2.0.00
πΈ bucket_alloc πΈ
The bucket allocator you use for literally all bucket/brigade creations πͺ£
$ba = $c->bucket_alloc();
- obj:
$c(Apache2::Connection object) - ret:
$ba(APR::BucketAlloc object) - since:
2.0.00
APR::Bucket and APR::Brigade methods/functions literally need this object, so don't lose it bestie.
πΈ client_socket πΈ
Get/set the client socket π
$socket = $c->client_socket;
$prev_socket = $c->client_socket($new_socket);
- obj:
$c(Apache2::Connection object) - opt arg1:
$new_socket(APR::Socket objectobject ) β if you pass a new socket, it gets set - ret:
$socket(APR::Socket objectobject ) β the current client socket. If the optional arg$new_socketwas passed, you get the previous socket object back instead - since:
2.0.00
πΈ get_remote_host πΈ
Lookup the client's DNS hostname or IP address ππ
$remote_host = $c->remote_host();
$remote_host = $c->remote_host($type);
$remote_host = $c->remote_host($type, $dir_config);
- obj:
$c(Apache2::Connection object) β the current connection, you know the vibe - opt arg1:
$type(:remotehost constant) β what kind of lookup to do:Apache2::Const::REMOTE_DOUBLE_REVβ will always force a DNS lookup, and also force a double reverse lookup, no matter what theHostnameLookupssetting says. The result is the (double reverse checked) hostname, orundefif any of the lookups fail πApache2::Const::REMOTE_HOSTβ returns the hostname, orundefif the hostname lookup fails. It'll force a DNS lookup according to theHostnameLookupssetting.Apache2::Const::REMOTE_NAMEβ returns the hostname, or the dotted quad if the hostname lookup fails. It'll force a DNS lookup according to theHostnameLookupssetting.Apache2::Const::REMOTE_NOLOOKUPβ is likeApache2::Const::REMOTE_NAMEexcept that a DNS lookup is never forced.- The default value is
Apache2::Const::REMOTE_NAMEβ¨
- opt arg2:
$dir_config(Apache2::ConfVector object) β the directory config vector from the request. It's needed to find the container where theHostnameLookupsdirective is set. To grab one for the current request, use$r->per_dir_config. By default,undefgets passed, and in that case it's the same as ifHostnameLookupswas set toOff. - ret:
$remote_host( string/undef ) β the remote hostname. If the config directiveHostNameLookupsis set to off, this returns the dotted decimal representation of the client's IP address instead. Might returnundefif the hostname isn't known. - since:
2.0.00
The result of the get_remote_host call gets cached in $c->remote_host. If that one is set, get_remote_host will hand it right back to you immediately, no checkups needed π
πΈ id πΈ
ID of this connection; unique at any point in time π
$id = $c->id();
- obj:
$c(Apache2::Connection object) - ret:
$id(integer) - since:
2.0.00
πΈ input_filters πΈ
Get/set the first filter in a linked list of protocol level input filters π
$input_filters = $c->input_filters();
$prev_input_filters = $c->input_filters($new_input_filters);
- obj:
$c(Apache2::Connection object) - opt arg1:
$new_input_filtersβ set a new value - ret:
$input_filters(Apache2::Filter object) β the first filter in the connection input filters chain. If$new_input_filterswas passed, you get the previous value back instead. - since:
2.0.00
For an example see: Bucket Brigades-based Protocol Module π
πΈ keepalive πΈ
This method answers the big question: should the connection be kept alive for another HTTP request after the current one wraps up? π€
$status = $c->keepalive();
$status = $c->keepalive($new_status);
- obj:
$c(Apache2::Connection object) - opt arg1:
$new_status(:conn_keepalive constant) β normally you should not mess with setting this option while handling the HTTP protocol. If you do (for example when sending your own headers set with$r->assbackwards) β take a look at theap_set_keepalive()function inhttpd-2.0/modules/http/http_protocol.c. - ret:
$status(:conn_keepalive constant) β bestie, this does not return true or false, it returns one of the states which you can compare against (:conn_keepalive constants). - since:
2.0.00
Unless you set this value yourself when implementing non-HTTP protocols, it's only relevant for HTTP requests.
For example:
use Apache2::RequestRec ();
use Apache2::Connection ();
use Apache2::Const -compile => qw(:conn_keepalive);
...
my $c = $r->connection;
if ($c->keepalive == Apache2::Const::CONN_KEEPALIVE) {
# do something
}
elsif ($c->keepalive == Apache2::Const::CONN_CLOSE) {
# do something else
}
elsif ($c->keepalive == Apache2::Const::CONN_UNKNOWN) {
# do yet something else
}
else {
# die "unknown state";
}
Notice that new states could be added later by Apache, so your code should make no assumptions and do things only if the desired state matches. no cap π
πΈ keepalives πΈ
How many requests were already served over the current connection π’
$served = $c->keepalives();
$served = $c->keepalives($new_served);
- obj:
$c(Apache2::Connection object) - opt arg1:
$new_served(integer) β set the number of served requests over the current connection. Normally you won't do that when handling HTTP requests. (But see below a note regarding$r->assbackwards.) - ret:
$served(integer) β how many requests were already served over the current connection. In most handlers, but HTTP output filter handlers, that value doesn't count the current request. For the latter it'll count the current request. - since:
2.0.00
This method is only relevant for keepalive connections. The core connection output filter ap_http_header_filter increments this value when the response headers are sent and it decides that the connection should not be closed (see ap_set_keepalive()).
If you send your own set of HTTP headers with $r->assbackwards, which includes the Keep-Alive HTTP response header, you must make sure to increment the keepalives counter π
πΈ local_addr πΈ
Get this connection's local socket address π
$local_sa = $c->local_addr();
- obj:
$c(Apache2::Connection object) - ret:
$local_sa(APR::SockAddr object) - since:
2.0.00
πΈ local_host πΈ
used for ap_get_server_name when UseCanonicalName is set to DNS (ignores setting of HostnameLookups) π·οΈ
$local_host = $c->local_host();
- obj:
$c(Apache2::Connection object) - ret:
$local_host(string) - since:
2.0.00
META: you probably shouldn't use this method, but ( get_server_name ) if inside request and $r is available.
πΈ local_ip πΈ
server IP address π₯οΈ
$local_ip = $c->local_ip();
- obj:
$c(Apache2::Connection object) - ret:
$local_ip(string) - since:
2.0.00
πΈ notes πΈ
Get/set text notes for the duration of this connection. These notes can be passed from one module to another (not only mod_perl, but modules in any other language) π
$notes = $c->notes();
$prev_notes = $c->notes($new_notes);
- obj:
$c(Apache2::Connection object) - opt arg1:
$new_notes(APR::Table object) - ret:
$notes(APR::Table object) β the current notes table. If the$new_notesargument was passed, you get the previous value back instead. - since:
2.0.00
Also see $r->notes π
πΈ output_filters πΈ
Get the first filter in a linked list of protocol level output filters π€
$output_filters = $c->output_filters();
$prev_output_filters = $r->output_filters($new_output_filters);
- obj:
$c(Apache2::Connection object) - opt arg1:
$new_output_filtersβ set a new value - ret:
$output_filters(Apache2::Filter object) β the first filter in the connection output filters chain. If$new_output_filterswas passed, you get the previous value back instead. - since:
2.0.00
For an example see: Bucket Brigades-based Protocol Module π
πΈ pool πΈ
Pool associated with this connection π
$p = $c->pool();
- obj:
$c(Apache2::Connection object) - ret:
$p(APR::Pool object) - since:
2.0.00
πΈ remote_addr πΈ
Get this connection's remote socket address π
$remote_sa = $c->remote_addr();
- obj:
$c(Apache2::Connection object) - ret:
$remote_sa(APR::SockAddr object) - since:
2.0.00
πΈ remote_ip πΈ
Client's IP address π
$remote_ip = $c->remote_ip();
$prev_remote_ip = $c->remote_ip($new_remote_ip);
- obj:
$c(Apache2::Connection object) - opt arg1:
$new_remote_ip( string ) β if passed, a new value will be set - ret:
$remote_ip( string ) β current remote ip address. If the optional argument$new_remote_ipwas passed, the previous value is returned instead. - since:
2.0.00
πΈ remote_host πΈ
Client's DNS name π·οΈ
$remote_host = $c->remote_host();
- obj:
$c(Apache2::Connection object) - ret:
$remote_host( string/undef ) β if$c->get_remote_hostwas run, it returns the cached value, which is a client DNS name or""if it wasn't found. If the check wasn't run βundefis returned. - since:
2.0.00
It's best to call $c->get_remote_host instead of directly accessing this variable, fr fr π
π§ Unsupported API π§
Okay bestie, real talk β Apache2::Connection also has an auto-generated Perl interface for a few other methods that nobody has tested yet π, so their API is subject to change. They'll get finalized later when somebody actually needs them, fr.
If you're out here wanting to rely on any of the methods below, please hit up the mod_perl development mailing list so we can all help each other do the steps necessary to move that method over to an officially supported API. Team effort, iykyk π«Ά
conn_config
Config vector holding pointers to connections per-server config structures. Very pointer-y. Very vector-y. Iconic π
$ret = $c->conn_config();
- obj:
$c(Apache2::Connection object) - ret:
$ret(Apache2::ConfVector object) - since:
2.0.00
sbh
META: Autogenerated β needs to be reviewed/completed βοΈ
This is a handle to scoreboard information for this connection πΉ
$sbh = $c->sbh();
- obj:
$c(Apache2::Connection object) - ret:
$sbh(XXX) - since:
2.0.00
META: Not sure how this can be used from mod_perl at the moment. Unless Apache2::Scoreboard is extended to provide a hook to read from this variable π
π See Also π
mod_perl 2.0 documentation. Go read it, bestie, it's giving required reading πβ¨
π Copyright π
mod_perl 2.0 and its core modules are copyrighted under The Apache Software License, Version 2.0.
π Authors π
The mod_perl development team and numerous contributors. Legends, every single one of them π«Ά
And that's a wrap on libapa...nection(3pm)! This whole thing is perl v5.42.3, dated 2026-08-20, straight from the User Contributed Perl Documentation β¨ Stay hydrated, keep your config vectors close, and remember: test the unsupported stuff before you build your whole life on it π
ππ¦
π¦ where this page comes from
- ubuntu stonking: libapache2-mod-perl2 2.0.13-2build3 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms