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