All pastes #746040 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-10-22 22:16 UTC
rendered paste body
/* PANW PANW STIN KLASI EXOUME GRAPSEI AFTES TIS ARXIKOPOIISEIS - EXEI KAI EPIPLEON POU DEN XREIAZETAI GI AFTO POU SAS DEIXNW*/

	private Socket clientConnection;			/* client's connection */
	private Socket serverConnection;			/* server's connection */
	private StringBuffer clientRequest;			/* client's received request */
	private StringBuffer remoteServerReplyHeaders;		/* server's reply headers */
	private Vector remoteServerReplyData;			/* server's reply data */
	private String remoteHost;				/* remote web server hostname */
	private String requestedPage;				/* use this for replication purposes */
	private byte [] outBytes;

	/* follow the streams which are used for communication purposes */
	private BufferedInputStream clientIn;
	private BufferedOutputStream clientOut;
	private BufferedInputStream serverIn;
	private BufferedOutputStream serverOut;

	/* cache these header lines for a more efficient parsing */
	private String firstLine;
	private String secondLine;

/* ME TIN PARAKATW SINARTISI SINDEOMASTE STON SERVER KAI TRAVAME TI SELIDA */

	/* connects with the remoted web server and gets the requested web page */
	private void getRequestedURL() throws IOException {
		/* do the appropriate initializations */
		serverConnection = new Socket(InetAddress.getByName(remoteHost), 80);
		getServerStreams();
		
		/* send the request to the remote web server */
		serverOut.write(clientRequest.toString().getBytes(), 0, clientRequest.toString().getBytes().length);
		serverOut.flush();

		/* get the headers */
		int c; int cnt = 0;
		while ((c = serverIn.read()) != -1) {
			remoteServerReplyHeaders.append((char)c); ++cnt;
			if (cnt > 4) {
				if (remoteServerReplyHeaders.toString().endsWith("\r\n\r\n")) {
					break;
				}
			}
		}
		
		/* find Content-Length field and extract the following integer */
		int pos = remoteServerReplyHeaders.toString().lastIndexOf(CONTENT_LENGTH) + CONTENT_LENGTH.length() + 1;
		String subStr = remoteServerReplyHeaders.toString().substring(pos);
		StringTokenizer token = new StringTokenizer(subStr);
		int contentLength = Integer.parseInt(token.nextToken());

		/* print remote server reply - for program verification purposes */
		System.out.println("\n------ HEADERS RECEIVED FROM REMOTED WEB-SERVER ------");
		System.out.println(remoteServerReplyHeaders.toString());
		System.out.println("------------------ END OF HEADERS --------------------\n");

		/* now get the data itself */
		int bytesRead = 0; int byteCount = 0;
		byte [] buf =  new byte[READ_UNIT]; /* read little chunck of bytes each time */
		
		while (byteCount < contentLength && (bytesRead = serverIn.read(buf, 0, READ_UNIT)) > 0 ) {
			byte [] myBuf = new byte[bytesRead];
			for ( int i = 0; i < bytesRead; i++ )
				myBuf[i] = buf[i];
			remoteServerReplyData.add(myBuf);
			byteCount += bytesRead;
		}

		/* store all of them in outBytes buffer */
		outBytes = new byte[byteCount];
		byte [] tempByte = null;
		int size = remoteServerReplyData.size(); int myCounter = 0;

		for ( int i = 0; i < size; i++ ) {
			Object obj = null;
			if ( (obj = remoteServerReplyData.elementAt(i)) instanceof byte[] ) {
				tempByte = (byte[]) obj;
				for ( int j = 0; j < tempByte.length; j++ )
					outBytes[myCounter + j] = tempByte[j];
				myCounter += tempByte.length;
			}
		}
	}