⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 message.pm

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 PM
📖 第 1 页 / 共 2 页
字号:
    # We create the function here so that it will not need to be    # autoloaded the next time.    no strict 'refs';    *$method = eval "sub { shift->{'_headers'}->$method(\@_) }";    goto &$method;}# Private method to access members in %$selfsub _elem{    my $self = shift;    my $elem = shift;    my $old = $self->{$elem};    $self->{$elem} = $_[0] if @_;    return $old;}# Create private _parts attribute from current _contentsub _parts {    my $self = shift;    my $ct = $self->content_type;    if ($ct =~ m,^multipart/,) {	require HTTP::Headers::Util;	my @h = HTTP::Headers::Util::split_header_words($self->header("Content-Type"));	die "Assert" unless @h;	my %h = @{$h[0]};	if (defined(my $b = $h{boundary})) {	    my $str = $self->content;	    $str =~ s/\r?\n--\Q$b\E--\r?\n.*//s;	    if ($str =~ s/(^|.*?\r?\n)--\Q$b\E\r?\n//s) {		$self->{_parts} = [map HTTP::Message->parse($_),				   split(/\r?\n--\Q$b\E\r?\n/, $str)]	    }	}    }    elsif ($ct eq "message/http") {	require HTTP::Request;	require HTTP::Response;	my $content = $self->content;	my $class = ($content =~ m,^(HTTP/.*)\n,) ?	    "HTTP::Response" : "HTTP::Request";	$self->{_parts} = [$class->parse($content)];    }    elsif ($ct =~ m,^message/,) {	$self->{_parts} = [ HTTP::Message->parse($self->content) ];    }    $self->{_parts} ||= [];}# Create private _content attribute from current _partssub _content {    my $self = shift;    my $ct = $self->header("Content-Type") || "multipart/mixed";    if ($ct =~ m,^\s*message/,i) {	_set_content($self, $self->{_parts}[0]->as_string($CRLF), 1);	return;    }    require HTTP::Headers::Util;    my @v = HTTP::Headers::Util::split_header_words($ct);    Carp::carp("Multiple Content-Type headers") if @v > 1;    @v = @{$v[0]};    my $boundary;    my $boundary_index;    for (my @tmp = @v; @tmp;) {	my($k, $v) = splice(@tmp, 0, 2);	if (lc($k) eq "boundary") {	    $boundary = $v;	    $boundary_index = @v - @tmp - 1;	    last;	}    }    my @parts = map $_->as_string($CRLF), @{$self->{_parts}};    my $bno = 0;    $boundary = _boundary() unless defined $boundary; CHECK_BOUNDARY:    {	for (@parts) {	    if (index($_, $boundary) >= 0) {		# must have a better boundary		$boundary = _boundary(++$bno);		redo CHECK_BOUNDARY;	    }	}    }    if ($boundary_index) {	$v[$boundary_index] = $boundary;    }    else {	push(@v, boundary => $boundary);    }    $ct = HTTP::Headers::Util::join_header_words(@v);    $self->header("Content-Type", $ct);    _set_content($self, "--$boundary$CRLF" .	                join("$CRLF--$boundary$CRLF", @parts) .			"$CRLF--$boundary--$CRLF",                        1);}sub _boundary{    my $size = shift || return "xYzZY";    require MIME::Base64;    my $b = MIME::Base64::encode(join("", map chr(rand(256)), 1..$size*3), "");    $b =~ s/[\W]/X/g;  # ensure alnum only    $b;}1;__END__=head1 NAMEHTTP::Message - HTTP style message (base class)=head1 SYNOPSIS use base 'HTTP::Message';=head1 DESCRIPTIONAn C<HTTP::Message> object contains some headers and a content body.The following methods are available:=over 4=item $mess = HTTP::Message->new=item $mess = HTTP::Message->new( $headers )=item $mess = HTTP::Message->new( $headers, $content )This constructs a new message object.  Normally you would wantconstruct C<HTTP::Request> or C<HTTP::Response> objects instead.The optional $header argument should be a reference to anC<HTTP::Headers> object or a plain array reference of key/value pairs.If an C<HTTP::Headers> object is provided then a copy of it will beembedded into the constructed message, i.e. it will not be owned andcan be modified afterwards without affecting the message.The optional $content argument should be a string of bytes.=item $mess = HTTP::Message->parse( $str )This constructs a new message object by parsing the given string.=item $mess->headersReturns the embedded C<HTTP::Headers> object.=item $mess->headers_as_string=item $mess->headers_as_string( $eol )Call the as_string() method for the headers in themessage.  This will be the same as    $mess->headers->as_stringbut it will make your program a whole character shorter :-)=item $mess->content=item $mess->content( $content )The content() method sets the raw content if an argument is given.  If noargument is given the content is not touched.  In either case theoriginal raw content is returned.Note that the content should be a string of bytes.  Strings in perlcan contain characters outside the range of a byte.  The C<Encode>module can be used to turn such strings into a string of bytes.=item $mess->add_content( $data )The add_content() methods appends more data to the end of the currentcontent buffer.=item $mess->content_ref=item $mess->content_ref( \$content )The content_ref() method will return a reference to content buffer string.It can be more efficient to access the content this way if the contentis huge, and it can even be used for direct manipulation of the content,for instance:  ${$res->content_ref} =~ s/\bfoo\b/bar/g;This example would modify the content buffer in-place.If an argument is passed it will setup the content to reference someexternal source.  The content() and add_content() methodswill automatically dereference scalar references passed this way.  Forother references content() will return the reference itself andadd_content() will refuse to do anything.=item $mess->decoded_content( %options )Returns the content with any C<Content-Encoding> undone and stringsmapped to perl's Unicode strings.  If the C<Content-Encoding> orC<charset> of the message is unknown this method will fail byreturning C<undef>.The following options can be specified.=over=item C<charset>This override the charset parameter for text content.  The valueC<none> can used to suppress decoding of the charset.=item C<default_charset>This override the default charset of "ISO-8859-1".=item C<raise_error>If TRUE then raise an exception if not able to decode content.  Reasonmight be that the specified C<Content-Encoding> or C<charset> is notsupported.  If this option is FALSE, then decode_content() will returnC<undef> on errors, but will still set $@.=item C<ref>If TRUE then a reference to decoded content is returned.  This mightbe more efficient in cases where the decoded content is identical tothe raw content as no data copying is required in this case.=back=item $mess->parts=item $mess->parts( @parts )=item $mess->parts( \@parts )Messages can be composite, i.e. contain other messages.  The compositemessages have a content type of C<multipart/*> or C<message/*>.  Thismethod give access to the contained messages.The argumentless form will return a list of C<HTTP::Message> objects.If the content type of $msg is not C<multipart/*> or C<message/*> thenthis will return the empty list.  In scalar context only the firstobject is returned.  The returned message parts should be regarded asare read only (future versions of this library might make it possibleto modify the parent by modifying the parts).If the content type of $msg is C<message/*> then there will only beone part returned.If the content type is C<message/http>, then the return value will beeither an C<HTTP::Request> or an C<HTTP::Response> object.If an @parts argument is given, then the content of the message willmodified. The array reference form is provided so that an empty listcan be provided.  The @parts array should contain C<HTTP::Message>objects.  The @parts objects are owned by $mess after this call andshould not be modified or made part of other messages.When updating the message with this method and the old content type of$mess is not C<multipart/*> or C<message/*>, then the content type isset to C<multipart/mixed> and all other content headers are cleared.This method will croak if the content type is C<message/*> and morethan one part is provided.=item $mess->add_part( $part )This will add a part to a message.  The $part argument should beanother C<HTTP::Message> object.  If the previous content type of$mess is not C<multipart/*> then the old content (together with allcontent headers) will be made part #1 and the content type madeC<multipart/mixed> before the new part is added.  The $part object isowned by $mess after this call and should not be modified or made partof other messages.There is no return value.=item $mess->clearWill clear the headers and set the content to the empty string.  Thereis no return value=item $mess->protocol=item $mess->protocol( $proto )Sets the HTTP protocol used for the message.  The protocol() is a stringlike C<HTTP/1.0> or C<HTTP/1.1>.=item $mess->cloneReturns a copy of the message object.=item $mess->as_string=item $mess->as_string( $eol )Returns the message formatted as a single string.The optional $eol parameter specifies the line ending sequence to use.The default is "\n".  If no $eol is given then as_string will ensurethat the returned string is newline terminated (even when the messagecontent is not).  No extra newline is appended if an explicit $eol ispassed.=backAll methods unknown to C<HTTP::Message> itself are delegated to theC<HTTP::Headers> object that is part of every message.  This allowsconvenient access to these methods.  Refer to L<HTTP::Headers> fordetails of these methods:    $mess->header( $field => $val )    $mess->push_header( $field => $val )    $mess->init_header( $field => $val )    $mess->remove_header( $field )    $mess->remove_content_headers    $mess->header_field_names    $mess->scan( \&doit )    $mess->date    $mess->expires    $mess->if_modified_since    $mess->if_unmodified_since    $mess->last_modified    $mess->content_type    $mess->content_encoding    $mess->content_length    $mess->content_language    $mess->title    $mess->user_agent    $mess->server    $mess->from    $mess->referer    $mess->www_authenticate    $mess->authorization    $mess->proxy_authorization    $mess->authorization_basic    $mess->proxy_authorization_basic=head1 COPYRIGHTCopyright 1995-2004 Gisle Aas.This library is free software; you can redistribute it and/ormodify it under the same terms as Perl itself.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -