package com.bloatit.framework.xcgiserver.postparsing.parsers;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import com.bloatit.common.Log;
import com.bloatit.framework.xcgiserver.postparsing.PostParameter;
import com.bloatit.framework.xcgiserver.postparsing.exceptions.MalformedPostException;

public class SimplePostParser extends PostParameterParser {
    private final int length;
    private final InputStream postStream;
    private String[] postData;
    private int i;

    public SimplePostParser(final InputStream postStream, final int length) {
        this.postStream = postStream;
        this.length = length;
    }

    public void initParsing() throws IOException {
        final byte[] postBytes = new byte[length];
        final int read = postStream.read(postBytes);
        if (read == length) {
            Log.framework().debug("Post value read correctly.");
        } else {
            Log.framework().fatal("Post is not of the expected size, we may encounter problems later during the page generation.");
        }

        final String string = new String(postBytes);
        postData = string.split("&");
        i = 0;
    }

    @Override
    public PostParameter readNext() throws MalformedPostException, IOException {
        if (length == 0) {
            return null;
        }
        if (postData == null) {
            initParsing();
        }
        if (i >= postData.length) {
            return null;
        }
        Log.framework().trace("Reading simple POST data n°" + i);
        final String param = postData[i];
        while (i < postData.length) {
            try {
                final String[] pair = param.split("=");
                if (pair.length >= 2) {
                    final String key = URLDecoder.decode(pair[0], "UTF-8");
                    String value;
                    value = URLDecoder.decode(pair[1], "UTF-8");
                    i++;
                    return new PostParameter(key, value);
                }
                i++;
            } catch (final UnsupportedEncodingException e) {
                Log.framework().fatal("Encoding is UTF-8 not supported. This is bad, check system parameters", e);
                throw new MalformedPostException("Encoding is UTF-8 not supported. This is bad, check system parameters", e);
            }
        }
        return null;
    }
}

@@ -1606,79 +1606,83 @@
     
-final String param = postData%5Bi%5D;%0A        while (i %3C postData.length) %7B
+while (i %3C postData.length) %7B%0A            final String param = postData%5Bi%5D;
 %0A   
@@ -1959,32 +1959,113 @@
 r%5B1%5D, %22UTF-8%22);%0A
+                    PostParameter postParameter = new PostParameter(key, value);%0A
                 
@@ -2100,21 +2100,17 @@
  return 
-new P
+p
 ostParam
@@ -2109,36 +2109,24 @@
 ostParameter
-(key, value)
 ;%0A          
@@ -2259,33 +2259,33 @@
 l(%22Encoding 
-is 
 UTF-8 
+is 
 not supporte
@@ -2393,17 +2393,17 @@
 ing 
-is 
 UTF-8 
+is 
 not 

fix bug in post parameter parsing (after null parameters are no longer ignored)