Skip to content

Modernize send_file_response to use 3-arg open with a lexical filehandle #83

@oalders

Description

@oalders

HTTP::Daemon::send_file_response currently uses an antique I/O idiom (lib/HTTP/Daemon.pm:565-567):

local (*F);
sysopen(F, $file, 0) or return $self->send_error(RC_FORBIDDEN);
binmode(F);

This predates the style perlopentut has recommended for many years: 3-arg open with an explicit mode and a lexical filehandle. The same tutorial deliberately stays away from sysopen, which is a lower-level primitive intended for cases that need O_* flag combinations (e.g. O_EXCL for atomic creates) — not plain read-only access.

Suggested replacement:

open(my $fh, '<', $file) or return $self->send_error(RC_FORBIDDEN);
binmode($fh);

Then pass $fh into send_file instead of \*F.

Notes:

  • The mode argument 0 is O_RDONLY as a bare magic number, which is harder to read than an explicit <.
  • The localized typeglob *F is only needed because the handle is a bareword; a lexical $fh removes that requirement entirely.
  • Behaviour is unchanged: 3-arg open with explicit < opens the file read-only and does not interpret any 2-arg open magic in the path.

Scope: this is a cleanup of send_file_response only. send_file already uses the modern form.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions