<commit_before>// PostData.java

/**
*    Copyright (C) 2008 10gen Inc.
*  
*    This program is free software: you can redistribute it and/or  modify
*    it under the terms of the GNU Affero General Public License, version 3,
*    as published by the Free Software Foundation.
*  
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Affero General Public License for more details.
*  
*    You should have received a copy of the GNU Affero General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package ed.net.httpserver;

import java.io.*;
import java.nio.*;
import java.util.*;
import java.util.regex.*;
import javax.servlet.*;

import ed.io.*;
import ed.js.*;
import ed.util.*;

public abstract class PostData {
    
    static final boolean DEBUG = false;

    static PostData create( HttpRequest req ){
        final int cl = req.getIntHeader( "Content-Length" , 0 );
        final String ct = req.getHeader( "Content-Type" );
        
        if ( cl <= 0 )
            return null;
        
        boolean multipart = ct != null && ct.toLowerCase().trim().startsWith( "multipart/form-data" );
        
        if ( ! multipart ){
            if ( cl > PostDataInMemory.MAX )
                throw new RuntimeException( "can't have regular post greater than 10 mb" );
            return new PostDataInMemory( cl , multipart , ct );
        }
        
        if ( cl > PostDataInMemory.MAX )
            throw new RuntimeException( "can't handle huge stuff yet" );
        
        return new PostDataInMemory( cl , multipart , ct );
    }


    PostData( int len , boolean multipart , String contentType ){
        _len = len;
        _multipart = multipart;
        _contentType = contentType;
        _boundary = _parseBoundary( _contentType );
    }

    public int length(){
        return _len;
    }

    public Set<String> filenames(){
        return _files.keySet();
    }


    abstract int position();
    abstract byte get( int pos );
    abstract void put( byte b );
    
    abstract String string( int start , int len );
    abstract void fillIn( ByteBuffer buf , int start , int end );

    public void writeTo( JSLocalFile f )
        throws IOException {
        writeTo( f.getRealFile() );
    }

    public abstract void writeTo( File f )
        throws IOException ;

    public abstract void write( OutputStream out , int start , int end )
        throws IOException;

    public ServletInputStream getInputStream(){
        // TODO: make this faster
        final int max = _len;
        return new ServletInputStream(){
            public int read(){
                if ( _pos >= max )
                    return -1;
                return get( _pos++ );
                    
            }
            
            int _pos;
        };
    }

    public ByteStream getByteStream(){
	final int max = _len;
	return new ByteStream(){
	    
	    public boolean hasMore(){
		return _pos < max;
	    }

	    public int write( ByteBuffer bb ){
		final int toTransfer = Math.min( bb.remaining() , max - _pos );

		fillIn( bb , _pos , _pos + toTransfer );

		_pos += toTransfer;
		return toTransfer;
	    }

	    private int _pos = 0;
	};
    }
    

    int indexOf( byte b[] , int start ){
        
        outer:
        for ( int i=start; i<_len-b.length; i++ ){
            for ( int j=0; j<b.length; j++ )
                if ( b[j] != get( i + j ) )
                    continue outer;

            return i;
        }
        return -1;
    }
    
    boolean done(){
        final int pos = position();
        if ( pos > _len )
            throw new RuntimeException( "something is wrong" );
        return pos == _len;
    }

    void go( HttpRequest req ){
        if ( ! _multipart ){
            addRegularParams( req );
            return;
        }
        
        parseMultiPart( req );
    }

    private void parseMultiPart( HttpRequest req ){
        if ( DEBUG ) System.out.println( "starting to parse multi-part" );

        int start = indexOf( _boundary , 0 );
        if ( start < 0 )
            return;
        
        start += _boundary.length + 1;

        while ( start < _len ){
            <commit_msg>gotSoFar<commit_after>// PostData.java

/**
*    Copyright (C) 2008 10gen Inc.
*  
*    This program is free software: you can redistribute it and/or  modify
*    it under the terms of the GNU Affero General Public License, version 3,
*    as published by the Free Software Foundation.
*  
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Affero General Public License for more details.
*  
*    You should have received a copy of the GNU Affero General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package ed.net.httpserver;

import java.io.*;
import java.nio.*;
import java.util.*;
import java.util.regex.*;
import javax.servlet.*;

import ed.io.*;
import ed.js.*;
import ed.util.*;

public abstract class PostData {
    
    static final boolean DEBUG = false;

    static PostData create( HttpRequest req ){
        final int cl = req.getIntHeader( "Content-Length" , 0 );
        final String ct = req.getHeader( "Content-Type" );
        
        if ( cl <= 0 )
            return null;
        
        boolean multipart = ct != null && ct.toLowerCase().trim().startsWith( "multipart/form-data" );
        
        if ( ! multipart ){
            if ( cl > PostDataInMemory.MAX )
                throw new RuntimeException( "can't have regular post greater than 10 mb" );
            return new PostDataInMemory( cl , multipart , ct );
        }
        
        if ( cl > PostDataInMemory.MAX )
            throw new RuntimeException( "can't handle huge stuff yet" );
        
        return new PostDataInMemory( cl , multipart , ct );
    }


    PostData( int len , boolean multipart , String contentType ){
        _len = len;
        _multipart = multipart;
        _contentType = contentType;
        _boundary = _parseBoundary( _contentType );
    }

    public int length(){
        return _len;
    }

    public Set<String> filenames(){
        return _files.keySet();
    }


    abstract int position();
    abstract byte get( int pos );
    abstract void put( byte b );
    
    abstract String string( int start , int len );
    abstract void fillIn( ByteBuffer buf , int start , int end );

    public void writeTo( JSLocalFile f )
        throws IOException {
        writeTo( f.getRealFile() );
    }

    public abstract void writeTo( File f )
        throws IOException ;

    public abstract void write( OutputStream out , int start , int end )
        throws IOException;

    public ServletInputStream getInputStream(){
        // TODO: make this faster
        final int max = _len;
        return new ServletInputStream(){
            public int read(){
                if ( _pos >= max )
                    return -1;
                return get( _pos++ );
                    
            }
            
            int _pos;
        };
    }

    public ByteStream getByteStream(){
	final int max = _len;
	return new ByteStream(){
	    
	    public boolean hasMore(){
		return _pos < max;
	    }

	    public int write( ByteBuffer bb ){
		final int toTransfer = Math.min( bb.remaining() , max - _pos );

		fillIn( bb , _pos , _pos + toTransfer );

		_pos += toTransfer;
		return toTransfer;
	    }

	    private int _pos = 0;
	};
    }
    

    int indexOf( byte b[] , int start ){
        
        outer:
        for ( int i=start; i<_len-b.length; i++ ){
            for ( int j=0; j<b.length; j++ )
                if ( b[j] != get( i + j ) )
                    continue outer;

            return i;
        }
        return -1;
    }
    
    boolean done(){
        final int pos = position();
        if ( pos > _len )
            throw new RuntimeException( "something is wrong" );
        return pos == _len;
    }

    int gotSoFar(){
	return position();
    }

    void go( HttpRequest req ){
        if ( ! _multipart ){
            addRegularParams( req );
            return;
        }
        
        parseMultiPart( req );
    }

    private void parseMultiPart( HttpRequest req ){
        if ( DEBUG ) System.out.println( "starting to parse multi-part" );

        int start = indexOf( _boundary , 0 );
        if ( start < 0 )
            return;
        
        start += _boundary.length + 1;

        while ( start < _len ){
            
{'fraction_non_alphanumeric': 0.09537374658158615, 'fraction_numerical': 0.004102096627164996, 'mean_word_length': 1.8059462915601023, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '238587', 'n_tokens_mistral': 2511, 'n_tokens_neox': 2377, 'n_words': 858}