Thread Sequentieller Parser f. Enctype="multipart/form-data" (1 answers)
Opened by janus at 2015-11-06 11:06

Gast janus
 2015-11-07 17:03
#182803 #182803
Ops da war noch ein Bug, hier isses richtig

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Parse eine einzelne Komponente
sub _parse_part{
    my $self = shift;
    my $head = shift;
    
    my $in   = $self->{stdin};
    my $boundary = $self->{boundary};
    
    # nun lese die binary
    my $tmp = IO::String->new;
    # boundary detection
    my @fifo = split('', $boundary);
    my $boundary_detect = 4;
    while( read($in, my $byte, 1)  ){
        shift @fifo;
        push @fifo, $byte;
        $tmp->print($byte);
        if( join('', @fifo) eq $boundary ){
            $boundary_detect = 2;
            last;
        }
    }

    my $content = '';
    $tmp->truncate( $tmp->tell() - length($boundary) - $boundary_detect );
    $tmp->seek(0,0);
    while(read($tmp, my $buffer, 1024)){
        $content .= $buffer;
    }    

    $tmp->seek(0,0);

    # parse header Angaben 
    my $content_type = do{
        $head =~ /Content-Type: ?(.*)/s;
        $1 ? unpack "A*", pack "A*", $1 : '';
    };
    my $name = do{
        $head =~ /name="(.*?)"/s;
        $1 ? $1 : '';
    };
    my $filename = do{
        $head =~ /filename="(.*?)"/s;
        $1 ? $1 : '';
    };

    push @{$self->{param}{$name}}, $filename ? {
        name           => $name,
        content_type   => $content_type,
        filename       => $filename,
        iohandle       => $tmp,
        content_length => length($content),
    } : $content;
}

Last edited: 2015-11-07 17:34:35 +0100 (CET)

View full thread Sequentieller Parser f. Enctype="multipart/form-data"