package org.nutz.http;

import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

import org.nutz.http.Request.METHOD;
import org.nutz.lang.Encoding;
import org.nutz.lang.Lang;

public class Http {

    public static class multipart {
        public static String getBoundary(String contentType) {
            if (null == contentType)
                return null;
            int pos = contentType.indexOf(";");
            if (pos <= 10)
                return null;
            if (!contentType.substring(0, pos).equalsIgnoreCase("multipart/form-data"))
                return null;
            pos = contentType.indexOf("=", pos);
            if (pos < 0)
                return null;
            return contentType.substring(pos + 1);
        }

        public static String formatName(String name, String filename, String contentType) {
            StringBuilder sb = new StringBuilder();
            sb.append("Content-Disposition: form-data; name=\"");
            sb.append(name);
            sb.append("\"");
            if (null != filename)
                sb.append("; filename=\"" + filename + "\"");
            if (null != contentType)
                sb.append("\nContent-Type: " + contentType);
            sb.append('\n' + '\n');
            return sb.toString();
        }

        public static String formatName(String name) {
            return formatName(name, null, null);
        }
    }

    public static Response get(String url) {
        return Sender.create(Request.get(url)).send();
    }

    public static Response get(String url, int timeout) {
        return Sender.create(Request.get(url)).setTimeout(timeout).send();
    }

    public static String post(String url, Map<String, Object> params, int timeout) {
        return Sender.create(Request.create(url, METHOD.POST, params, null))
                     .setTimeout(timeout)
                     .send()
                     .getContent();
    }

    public static Response post2(String url, Map<String, Object> params, int timeout) {
        return Sender.create(Request.create(url, METHOD.POST, params, null))
                     .setTimeout(timeout)
                     .send();
    }

    public static String encode(Object s) {
        if (null == s)
            return "";
        try {
            // Fix issue 283, 按照“茶几”的意见再次修改
            return URLEncoder.encode(s.toString(), Encoding.CHARSET_UTF8.name());
        }
        catch (UnsupportedEncodingException e) {
            throw Lang.wrapThrow(e);
        }
    }
    
    public static String post(String url, Map<String, Object> params, String inenc, String reenc) {
    	return Sender.create(Request.create(url, METHOD.POST, params, null).setEnc(inenc)).send().getContent(reenc);
    }

    public static ProxySwitcher proxySwitcher;

    public static void setHttpProxy(String host, int port) {
        final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
        proxySwitcher = new ProxySwitcher() {
            public Proxy getProxy(URL url) {
                return proxy;
            }
            public Proxy getProxy(Request req) {
            	req.getHeader().set("Connection", "close");
            	return getProxy(req.getUrl());
            }
        };
    }

    public static void setSocktProxy(String host, int port) {
        final Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(host, port));
        proxySwitcher = new ProxySwitcher() {
            public Proxy getProxy(URL url) {
                return proxy;
            }
            public Proxy getProxy(Request req) {
            	req.getHeader().set("Connection", "close");
            	return getProxy(req.getUrl());
            }
        };
    }

    public static ProxySwitcher getProxySwitcher() {
        return proxySwitcher;
    }

    public static void setProxySwitcher(ProxySwitcher proxySwitcher) {
        Http.proxySwitcher = proxySwitcher;
    }
}

@@ -509,296 +509,177 @@
     
-int pos = contentType.indexOf(%22;%22);%0D%0A            if (pos %3C= 10)%0D%0A                return null;%0D%0A            if (!contentType.substring(0, pos).equalsIgnoreCase(%22multipart/form-data%22))%0D%0A                return null;%0D%0A            pos = contentType.indexOf(%22=%22, pos);%0D%0A            if (pos %3C 0)
+for (String tmp : contentType.split(%22;%22)) %7B%0D%0A%09%09%09%09tmp = tmp.trim();%0D%0A%09%09%09%09if (tmp.startsWith(%22boundary=%22)) %7B%0D%0A%09%09%09%09%09return tmp.substring(%22boundary=%22.length());%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D
 %0D%0A  
@@ -680,36 +680,32 @@
 %09%7D%0D%0A            
- 
-   
 return null;%0D%0A  
@@ -706,60 +706,8 @@
 l;%0D%0A
-            return contentType.substring(pos + 1);%0D%0A
     

fix #637