    001package net.minecraft.client.mco;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.io.IOException;
    006import java.io.InputStream;
    007import java.net.HttpURLConnection;
    008import java.net.URL;
    009
    010@SideOnly(Side.CLIENT)
    011public abstract class Request
    012{
    013    protected HttpURLConnection field_96367_a;
    014    private boolean field_96366_c;
    015    protected String field_96365_b;
    016
    017    public Request(String par1Str, int par2, int par3)
    018    {
    019        try
    020        {
    021            this.field_96365_b = par1Str;
    022            this.field_96367_a = (HttpURLConnection)(new URL(par1Str)).openConnection();
    023            this.field_96367_a.setConnectTimeout(par2);
    024            this.field_96367_a.setReadTimeout(par3);
    025        }
    026        catch (Exception exception)
    027        {
    028            throw new ExceptionMcoHttp("Failed URL: " + par1Str, exception);
    029        }
    030    }
    031
    032    public void func_100006_a(String par1Str, String par2Str)
    033    {
    034        String s2 = this.field_96367_a.getRequestProperty("Cookie");
    035
    036        if (s2 == null)
    037        {
    038            this.field_96367_a.setRequestProperty("Cookie", par1Str + "=" + par2Str);
    039        }
    040        else
    041        {
    042            this.field_96367_a.setRequestProperty("Cookie", s2 + ";" + par1Str + "=" + par2Str);
    043        }
    044    }
    045
    046    public int func_96362_a()
    047    {
    048        try
    049        {
    050            this.func_96354_d();
    051            return this.field_96367_a.getResponseCode();
    052        }
    053        catch (Exception exception)
    054        {
    055            throw new ExceptionMcoHttp("Failed URL: " + this.field_96365_b, exception);
    056        }
    057    }
    058
    059    public McoOption func_98175_b()
    060    {
    061        String s = this.field_96367_a.getHeaderField("Set-Cookie");
    062
    063        if (s != null)
    064        {
    065            String s1 = s.substring(0, s.indexOf("="));
    066            String s2 = s.substring(s.indexOf("=") + 1, s.indexOf(";"));
    067            return McoOption.func_98153_a(McoPair.func_98157_a(s1, s2));
    068        }
    069        else
    070        {
    071            return McoOption.func_98154_b();
    072        }
    073    }
    074
    075    public String func_96364_c()
    076    {
    077        try
    078        {
    079            this.func_96354_d();
    080            String s = this.func_96362_a() >= 400 ? this.func_96352_a(this.field_96367_a.getErrorStream()) : this.func_96352_a(this.field_96367_a.getInputStream());
    081            this.func_96360_f();
    082            return s;
    083        }
    084        catch (IOException ioexception)
    085        {
    086            throw new ExceptionMcoHttp("Failed URL: " + this.field_96365_b, ioexception);
    087        }
    088    }
    089
    090    private String func_96352_a(InputStream par1InputStream) throws IOException
    091    {
    092        if (par1InputStream == null)
    093        {
    094            throw new IllegalArgumentException("input stream cannot be null");
    095        }
    096        else
    097        {
    098            StringBuilder stringbuilder = new StringBuilder();
    099
    100            for (int i = par1InputStream.read(); i != -1; i = par1InputStream.read())
    101            {
    102                stringbuilder.append((char)i);
    103            }
    104
    105            return stringbuilder.toString();
    106        }
    107    }
    108
    109    private void func_96360_f()
    110    {
    111        byte[] abyte = new byte[1024];
    112        InputStream inputstream;
    113
    114        try
    115        {
    116            boolean flag = false;
    117            inputstream = this.field_96367_a.getInputStream();
    118
    119            while (true)
    120            {
    121                if (inputstream.read(abyte) <= 0)
    122                {
    123                    inputstream.close();
    124                    break;
    125                }
    126            }
    127        }
    128        catch (Exception exception)
    129        {
    130            try
    131            {
    132                inputstream = this.field_96367_a.getErrorStream();
    133                boolean flag1 = false;
    134
    135                while (true)
    136                {
    137                    if (inputstream.read(abyte) <= 0)
    138                    {
    139                        inputstream.close();
    140                        break;
    141                    }
    142                }
    143            }
    144            catch (IOException ioexception)
    145            {
    146                ;
    147            }
    148        }
    149    }
    150
    151    protected Request func_96354_d()
    152    {
    153        if (!this.field_96366_c)
    154        {
    155            Request request = this.func_96359_e();
    156            this.field_96366_c = true;
    157            return request;
    158        }
    159        else
    160        {
    161            return this;
    162        }
    163    }
    164
    165    protected abstract Request func_96359_e();
    166
    167    public static Request func_96358_a(String par0Str)
    168    {
    169        return new RequestGet(par0Str, 5000, 5000);
    170    }
    171
    172    public static Request func_96361_b(String par0Str, String par1Str)
    173    {
    174        return new RequestPost(par0Str, par1Str.getBytes(), 5000, 5000);
    175    }
    176
    177    public static Request func_96355_b(String par0Str)
    178    {
    179        return new RequestDelete(par0Str, 5000, 5000);
    180    }
    181
    182    public static Request func_96363_c(String par0Str, String par1Str)
    183    {
    184        return new RequestPut(par0Str, par1Str.getBytes(), 5000, 5000);
    185    }
    186
    187    public static Request func_96353_a(String par0Str, String par1Str, int par2, int par3)
    188    {
    189        return new RequestPut(par0Str, par1Str.getBytes(), par2, par3);
    190    }
    191}