    001    package net.minecraft.src;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005    import java.io.BufferedReader;
    006    import java.io.DataOutputStream;
    007    import java.io.File;
    008    import java.io.IOException;
    009    import java.io.InputStreamReader;
    010    import java.io.UnsupportedEncodingException;
    011    import java.net.HttpURLConnection;
    012    import java.net.ServerSocket;
    013    import java.net.URL;
    014    import java.net.URLEncoder;
    015    import java.util.Iterator;
    016    import java.util.Map;
    017    import java.util.Map.Entry;
    018    import java.util.logging.Level;
    019    import java.util.logging.Logger;
    020
    021    public class HttpUtil
    022    {
    023        /**
    024         * Builds an encoded HTTP POST content string from a string map
    025         */
    026        public static String buildPostString(Map par0Map)
    027        {
    028            StringBuilder var1 = new StringBuilder();
    029            Iterator var2 = par0Map.entrySet().iterator();
    030
    031            while (var2.hasNext())
    032            {
    033                Entry var3 = (Entry)var2.next();
    034
    035                if (var1.length() > 0)
    036                {
    037                    var1.append('&');
    038                }
    039
    040                try
    041                {
    042                    var1.append(URLEncoder.encode((String)var3.getKey(), "UTF-8"));
    043                }
    044                catch (UnsupportedEncodingException var6)
    045                {
    046                    var6.printStackTrace();
    047                }
    048
    049                if (var3.getValue() != null)
    050                {
    051                    var1.append('=');
    052
    053                    try
    054                    {
    055                        var1.append(URLEncoder.encode(var3.getValue().toString(), "UTF-8"));
    056                    }
    057                    catch (UnsupportedEncodingException var5)
    058                    {
    059                        var5.printStackTrace();
    060                    }
    061                }
    062            }
    063
    064            return var1.toString();
    065        }
    066
    067        /**
    068         * Sends a HTTP POST request to the given URL with data from a map
    069         */
    070        public static String sendPost(URL par0URL, Map par1Map, boolean par2)
    071        {
    072            return sendPost(par0URL, buildPostString(par1Map), par2);
    073        }
    074
    075        /**
    076         * Sends a HTTP POST request to the given URL with data from a string
    077         */
    078        public static String sendPost(URL par0URL, String par1Str, boolean par2)
    079        {
    080            try
    081            {
    082                HttpURLConnection var3 = (HttpURLConnection)par0URL.openConnection();
    083                var3.setRequestMethod("POST");
    084                var3.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    085                var3.setRequestProperty("Content-Length", "" + par1Str.getBytes().length);
    086                var3.setRequestProperty("Content-Language", "en-US");
    087                var3.setUseCaches(false);
    088                var3.setDoInput(true);
    089                var3.setDoOutput(true);
    090                DataOutputStream var4 = new DataOutputStream(var3.getOutputStream());
    091                var4.writeBytes(par1Str);
    092                var4.flush();
    093                var4.close();
    094                BufferedReader var5 = new BufferedReader(new InputStreamReader(var3.getInputStream()));
    095                StringBuffer var7 = new StringBuffer();
    096                String var6;
    097
    098                while ((var6 = var5.readLine()) != null)
    099                {
    100                    var7.append(var6);
    101                    var7.append('\r');
    102                }
    103
    104                var5.close();
    105                return var7.toString();
    106            }
    107            catch (Exception var8)
    108            {
    109                if (!par2)
    110                {
    111                    Logger.getLogger("Minecraft").log(Level.SEVERE, "Could not post to " + par0URL, var8);
    112                }
    113
    114                return "";
    115            }
    116        }
    117
    118        @SideOnly(Side.CLIENT)
    119
    120        /**
    121         * The downloader for texturepacks stored in the server.
    122         */
    123        public static void downloadTexturePack(File par0File, String par1Str, IDownloadSuccess par2IDownloadSuccess, Map par3Map, int par4, IProgressUpdate par5IProgressUpdate)
    124        {
    125            Thread var6 = new Thread(new HttpUtilRunnable(par5IProgressUpdate, par1Str, par3Map, par0File, par2IDownloadSuccess, par4));
    126            var6.setDaemon(true);
    127            var6.start();
    128        }
    129
    130        @SideOnly(Side.CLIENT)
    131        public static int func_76181_a() throws IOException
    132        {
    133            ServerSocket var0 = null;
    134            boolean var1 = true;
    135            int var10;
    136
    137            try
    138            {
    139                var0 = new ServerSocket(0);
    140                var10 = var0.getLocalPort();
    141            }
    142            finally
    143            {
    144                try
    145                {
    146                    if (var0 != null)
    147                    {
    148                        var0.close();
    149                    }
    150                }
    151                catch (IOException var8)
    152                {
    153                    ;
    154                }
    155            }
    156
    157            return var10;
    158        }
    159    }