java - Socket proxy server redirecting url -


i have basic socket proxy server, want add redirection functionality in it. idea how go. changing host doesn't because buffer collected client contains old information.

import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.net.serversocket; import java.net.socket;  public class simpleproxyserver {      public static final int portnumber = 5555;      public static void main(string[] args) {         simpleproxyserver proxyserver = new simpleproxyserver();         proxyserver.start();      }      public void start() {         system.out.println("starting simpleproxyserver ...");         try {              serversocket serversocket = new serversocket(                     simpleproxyserver.portnumber, 1);             byte[] buffer = new byte[10000];              while (true) {                 socket clientsocket = serversocket.accept();                  inputstream bis = clientsocket.getinputstream();                   int n = bis.read(buffer);                 string browserrequest = new string(buffer, 0, n);                 system.out.println(browserrequest);                  int start = browserrequest.indexof("host: ") + 6;                 int end = browserrequest.indexof('\n', start);                 string host = browserrequest.substring(start, end - 1);                 system.out.println("connecting host " + host);                   socket hostsocket = new socket(host, 80); //i can change host on here                 outputstream sos = hostsocket.getoutputstream();                 system.out.println("forwarding request server");                 sos.write(buffer, 0, n);// buffer fetched client remains same                 sos.flush();                   inputstream sis = hostsocket.getinputstream();                 outputstream bos = clientsocket.getoutputstream();                  system.out.println("forwarding request server");                  {                     n = sis.read(buffer);                     system.out.println("receiving " + n + " bytes");                     if (n > 0) {                         bos.write(buffer, 0, n);                     }                 } while (n > 0);                  bos.flush();                 hostsocket.close();                 clientsocket.close();                 system.out.println("end of communication");              }         } catch (ioexception e) {             e.printstacktrace();         }     } } 

here client browser, buffer contains lots of data.

any example of browerrequest

get http://www.yahoo.com/ http/1.0 host: www.yahoo.com user-agent: mozilla/5.0 (windows nt 6.1; rv:16.0) gecko/20100101 firefox/16.0 accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 accept-language: en-us,en;q=0.5 accept-encoding: gzip, deflate connection: close proxy-connection: close cookie: b=c6r6cn99536pm&b=3&s=ha; fpc=d=medlilxmtebszkeht9narn_lmo6dbf2hc4azwnyveptqphb86midh7bpbylrlchwtcznc4rznuwz5r_xvfr6wjycyjnpg_abqekaftbksmcusblqqtm1ivzprz7m6pyiqtwtcogvas55ihfoy0eaqxdfeif8zkxrgpiyjmy2qm_wkorpt5txczkj.nkcmcuqh7aotva-&v=2; msc=t=1381079862x; billboard=nv=1&bn=0&collapsed=0; ypcdb=e5e21a3dacf7b87ec358a08261d310ec 

the next time when while loop again iterates browserreqest becomes,

get http://in.yahoo.com/?p=us http/1.0 host: in.yahoo.com user-agent: mozilla/5.0 (windows nt 6.1; rv:16.0) gecko/20100101 firefox/16.0 accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 accept-language: en-us,en;q=0.5 accept-encoding: gzip, deflate connection: close proxy-connection: close cookie: b=c6r6cn99536pm&b=3&s=ha; msc=t=1381079862x; fpc=d=nvqlqy5mteynypfx54.9vmbbb6hmzgzbtkpuzrh5pyn7mddabbwv96dq9xhvhlfj_a.dbwrkiwuiufasj8gs1ebtj3slzb31krhnjpe5leagmleexuijtqpyznfwrowafjcqcd_4sv90e1xeu87kcbt7lb1rzodbky1zuhmc57o0qejwl8rjjcecaodec.69elh3caikmaxkp5rwwbx0fk15zccybjromwwfxovaybfr.re27s4saqnucbqsfi3ro80k_ci5&v=2; fpps=_page=%7b%22wsid%22%3a%2221445690%22%7d; fpt=d=dv0pgaxxevzrfc2gezjlqqpfzziygbbbhdpw.vmtd6yhoqdqacyqilpwz..0n0jup35yo9s9yw8rmuxle9gjz1ra3mhzyfnymn67b_8cb7d7nbrcezs4azdavqbhb_9tfyeznojpm3auzqgudfg2fnomvl4oqkm_a7mt3vv8u4qr38gb864wfl6nxoyz7n_t1fa89lqfswvob0oozuxwnfied_jikxauj.3nfdkbkfh6qatpbxhlgp7ove7ibc0il.66u7kddaawveossyh1tv3lhsryshhs5mfslhpksujsp4libnxcqqrjwbmw27rl9mfg.r7vaoqwfhi6_24d0xtqdhkntlo2swscplscg35s353ibzprzranuwp_fyp3xuepv6e8yakenscbmx5..vjqtlomeef5mulnmfvqoxsrzlvog5vadrcpmellbxs6mzxmd822gifvp063z9df8lnrwztck8mfq0ob4xsuplzwbby6lb2yjiz57ddogxho6phfy59rqlqfpwsb38a9k3uu0tsjpdeg9wjffmmdew--&v=1; fpc_s=d=2smdyqjmteb.myibcno48gecpgkdd8rlrwyk4yiledpel8rmu.m9ox3wgvwegpvaqbcyslyjemektehpqomadgc.ekt3dwzmsyumo48ozjekv2z_fkiimkipqjsemcfngcwj8tjl_r2hzbs6z3mjjwmd28zabdgjrhj71rmxmfkj6ofas8r4ukkcxvyggvsygkxoeyip45umltdfvpba7snz_gacrhtpvlv6prjf4ffkxtpadx5ugixok_5s7umgpd8iqr.sokz0uhar3ibl6wpovgihvpvoqpaz8pajapgqwmp85ul3ujyqpxanb0b.wxaiyuxr6dlqbzuvrxhcn0pm5_pq6yatqiq0jbzsqqzndac8jaqeclmgrqua7e_7b60isewv7tiq1mddnhdgik5lbincwwlyvmouypxi0.uycpjw&v=2; fpck2=agbsuzsqabpeeabfmhaarigqagmc; billboard=nv=1&bn=0&collapsed=0; ypcdb=e5e21a3dacf7b87ec358a08261d310ec 

i want redirect request of www.yahoo.com lets www.google.com

how achieve ? because can see cannot edit buffer received client/browser.

you don't need content. client route request embedded urls in same way routed request url proxying: you.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -