    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.MalformedURLException;
    013    import java.net.ServerSocket;
    014    import java.net.URL;
    015    import java.net.URLEncoder;
    016    import java.util.HashMap;
    017    import java.util.Iterator;
    018    import java.util.Map;
    019    import java.util.Map.Entry;
    020    import java.util.logging.Level;
    021    import java.util.logging.Logger;
    022
    023    public class HttpUtil
    024    {
    025        /**
    026         * Builds an encoded HTTP POST content string from a string map
    027         */
    028        public static String buildPostString(Map par0Map)
    029        {
    030            StringBuilder var1 = new StringBuilder();
    031            Iterator var2 = par0Map.entrySet().iterator();
    032
    033            while (var2.hasNext())
    034            {
    035                Entry var3 = (Entry)var2.next();
    036
    037                if (var1.length() > 0)
    038                {
    039                    var1.append('&');
    040                }
    041
    042                try
    043                {
    044                    var1.append(URLEncoder.encode((String)var3.getKey(), "UTF-8"));
    045                }
    046                catch (UnsupportedEncodingException var6)
    047                {
    048                    var6.printStackTrace();
    049                }
    050
    051                if (var3.getValue() != null)
    052                {
    053                    var1.append('=');
    054
    055                    try
    056                    {
    057                        var1.append(URLEncoder.encode(var3.getValue().toString(), "UTF-8"));
    058                    }
    059                    catch (UnsupportedEncodingException var5)
    060                    {
    061                        var5.printStackTrace();
    062                    }
    063                }
    064            }
    065
    066            return var1.toString();
    067        }
    068
    069        /**
    070         * Sends a HTTP POST request to the given URL with data from a map
    071         */
    072        public static String sendPost(URL par0URL, Map par1Map, boolean par2)
    073        {
    074            return sendPost(par0URL, buildPostString(par1Map), par2);
    075        }
    076
    077        /**
    078         * Sends a HTTP POST request to the given URL with data from a string
    079         */
    080        public static String sendPost(URL par0URL, String par1Str, boolean par2)
    081        {
    082            try
    083            {
    084                HttpURLConnection var3 = (HttpURLConnection)par0URL.openConnection();
    085                var3.setRequestMethod("POST");
    086                var3.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    087                var3.setRequestProperty("Content-Length", "" + par1Str.getBytes().length);
    088                var3.setRequestProperty("Content-Language", "en-US");
    089                var3.setUseCaches(false);
    090                var3.setDoInput(true);
    091                var3.setDoOutput(true);
    092                DataOutputStream var4 = new DataOutputStream(var3.getOutputStream());
    093                var4.writeBytes(par1Str);
    094                var4.flush();
    095                var4.close();
    096                BufferedReader var5 = new BufferedReader(new InputStreamReader(var3.getInputStream()));
    097                StringBuffer var7 = new StringBuffer();
    098                String var6;
    099
    100                while ((var6 = var5.readLine()) != null)
    101                {
    102                    var7.append(var6);
    103                    var7.append('\r');
    104                }
    105
    106                var5.close();
    107                return var7.toString();
    108            }
    109            catch (Exception var8)
    110            {
    111                if (!par2)
    112                {
    113                    Logger.getLogger("Minecraft").log(Level.SEVERE, "Could not post to " + par0URL, var8);
    114                }
    115
    116                return "";
    117            }
    118        }
    119
    120        @SideOnly(Side.CLIENT)
    121
    122        /**
    123         * The downloader for texturepacks stored in the server.
    124         */
    125        public static void downloadTexturePack(File par0File, String par1Str, IDownloadSuccess par2IDownloadSuccess, Map par3Map, int par4, IProgressUpdate par5IProgressUpdate)
    126        {
    127            Thread var6 = new Thread(new HttpUtilRunnable(par5IProgressUpdate, par1Str, par3Map, par0File, par2IDownloadSuccess, par4));
    128            var6.setDaemon(true);
    129            var6.start();
    130        }
    131
    132        @SideOnly(Side.CLIENT)
    133        public static int func_76181_a() throws IOException
    134        {
    135            ServerSocket var0 = null;
    136            boolean var1 = true;
    137            int var10;
    138
    139            try
    140            {
    141                var0 = new ServerSocket(0);
    142                var10 = var0.getLocalPort();
    143            }
    144            finally
    145            {
    146                try
    147                {
    148                    if (var0 != null)
    149                    {
    150                        var0.close();
    151                    }
    152                }
    153                catch (IOException var8)
    154                {
    155                    ;
    156                }
    157            }
    158
    159            return var10;
    160        }
    161
    162        @SideOnly(Side.CLIENT)
    163        public static String[] func_82718_a(String par0Str, String par1Str)
    164        {
    165            HashMap var2 = new HashMap();
    166            var2.put("user", par0Str);
    167            var2.put("password", par1Str);
    168            var2.put("version", Integer.valueOf(13));
    169            String var3;
    170
    171            try
    172            {
    173                var3 = sendPost(new URL("http://login.minecraft.net/"), var2, false);
    174            }
    175            catch (MalformedURLException var5)
    176            {
    177                var5.printStackTrace();
    178                return null;
    179            }
    180
    181            if (var3 != null && var3.length() != 0)
    182            {
    183                if (!var3.contains(":"))
    184                {
    185                    if (var3.trim().equals("Bad login"))
    186                    {
    187                        System.out.println("Login failed");
    188                    }
    189                    else if (var3.trim().equals("Old version"))
    190                    {
    191                        System.out.println("Outdated launcher");
    192                    }
    193                    else if (var3.trim().equals("User not premium"))
    194                    {
    195                        System.out.println(var3);
    196                    }
    197                    else
    198                    {
    199                        System.out.println(var3);
    200                    }
    201
    202                    return null;
    203                }
    204                else
    205                {
    206                    String[] var4 = var3.split(":");
    207                    return new String[] {var4[2], var4[3]};
    208                }
    209            }
    210            else
    211            {
    212                System.out.println("Can\'t connect to minecraft.net");
    213                return null;
    214            }
    215        }
    216    }