📄 netlayer.java
字号:
/*
* j2wap is distributed under the terms of the GNU Public License
*
* j2wap was written by:
*
* Scott Campbell
* Michael Nordling
* Karl Maftoum
* Julian Bright
*
* This was a final project for Comp.Engineering at the University of Canberra, Australia
*
* Now released as Open Source software. 28 November 2000
*
* Email: k_maftoum@hotmail.com for more info
*
* Use entirely at your own risk.
*/
/*
This is the UDP NetLayer for j2wap
Original Version
GPL 20/8/00 Karl Maftoum
*/
/* Revision History
Version 0.0.1 20/8/00
Version 0.0.2 24/8/00
* Massive code cleanup
* Setup gateway and URI strings
* Working Content (Finally)
Version 0.0.3 27/8/00
* POST code added
Version 0.0.4 4/9/00
* Implementing Timeout
* All encoding accept bytes etc now work
* commented out Accept wbmp (not implemented yet)
Version 0.0.5 7/9/00
* Total re-write to implement POST & GET as one function for size
* Works except I can't yet test POST (no sites)
* IP Layer Header Deleted from Byte Array
Version 0.0.6 19/09/00
* Changed to add gc() calls, and less array copies
Version 0.0.7 2/10/00
* Removed debugging System.out.println calls
* Adding threading & exception handling for UI Cancel
* Getting the timeout to work properly
Version 0.0.8 6/10/00
* Fix the http:// substitution to work correctly
* Removed excessive gc() calls
* UI Cancel still doesn't work
* TID Now handled properly
Version 0.0.9 17/11/00
* Fixed all the bits from the Windows port back to Palm
* Got all redirects/timeouts/wierd things working.
* Removed UI Cancel threading, just didn't work properly
* This is the final version I think
Version 0.1.0 18/11/00
* Removed all debugging code
* Code Styling, and formatting
Version Windows 0.0.1 22/10/00
* Removed all Palm specific stuff
Version Windows 1.1 18/11/00
* Synched Palm & WIN32 Versions.
* See Palm Revision History for more details.
*/
package net;
import java.io.*;
import java.net.*;
/**
* Title: NetLayer
* Description: The NetLayer provdides all of the net services to j2wap
* Company: J2wap.com
* @author Karl Maftoum
* @version 1.1
*/
public class NetLayer {
static int TID = 2;
String httpsub;
String FinalURI;
public wae.CurrentDeck c_clsCurrDeck;
byte[] rbuf;
public byte[] doConnection(String gateway, String URI, String type, int oldTID, String Data) {
boolean bReturn = true;
ByteArrayOutputStream dg = new ByteArrayOutputStream();
try {
int sub = URI.indexOf("http://");
if(sub == -1) {
httpsub = "http://";
FinalURI = httpsub + URI;
}
else {
FinalURI = URI;
}
byte plen = (byte)FinalURI.length();
byte postMsg[] = FinalURI.getBytes();
/* Begin New (0.0.5) Code */
/* Trasaction ID Fixed now 0.0.8 */
TID++;
dg.write(TID); /* Transaction ID */
if(type.equals("GET")) {
dg.write(0x40); /* Indicates GET PDU */
dg.write(plen);
}
if(type.equals("POST")) {
dg.write(0x60); /* POST PDU */
dg.write(plen);
}
for(int i = 0;i < postMsg.length;i++) {
dg.write(postMsg[i]);
}
postMsg = null;
if(type.equals("POST")) {
dg.write(0x92); /* x-form-url-encoded */
}
dg.write(0xA9); /* User-Agent */
/* j2wap blessing code, i.e. HTTP Headers*/
String eMsg = "j2wap WIN32 1.1:See j2wap.com for details";
byte epostMsg[] = eMsg.getBytes();
for(int i = 0;i < epostMsg.length;i++) {
dg.write(epostMsg[i]);
}
epostMsg = null;
dg.write(0x00); /* Terminate Agent String */
dg.write(0x80); /* Accept */
dg.write(0x94); /* application/vnd.wap.wmlc */
/* UTF 8 - Required */
dg.write(0x81); /* Accept-Charset */
dg.write(0xEA); /* UTF 8 */
/* Standard, Latin-1 Charset */
dg.write(0x81); /* Accept-Charset */
dg.write(0x84); /* iso-8859-1 */
if(type.equals("POST")) {
byte postData[] = Data.getBytes();
for(int i = 0;i < postData.length;i++) {
dg.write(postData[i]);
}
postData = null;
}
byte[] buf = dg.toByteArray();
int length = buf.length;
InetAddress inet = InetAddress.getByName(gateway);
DatagramPacket dgp = new DatagramPacket(buf, length, inet, 9200);
DatagramSocket dgs = new DatagramSocket();
dgs.send(dgp);
byte rbuf[] = new byte[1500];
DatagramPacket dgr = new DatagramPacket(rbuf, 1500);
dgs.setSoTimeout(15000);
/* If we receive nothing within 15 seconds, give up */
try {
dgs.receive(dgr);
}
catch(InterruptedIOException e) {
byte[] failed = {
/**
* <wml>
* <card>
* <p>
* The request has timed out. Please enter a new URL.
* </p>
* </card>
* </wml>
*/
(byte)0x0002, (byte)0x0008, (byte)0x006a, (byte)0x0000,
(byte)0x0000, (byte)0x007f, (byte)0x00e7, (byte)0x0055,
(byte)0x0003, (byte)'T', (byte)'i', (byte)'m', (byte)'e',
(byte)'o', (byte)'u', (byte)'t', (byte)0x0000, (byte)0x0001,
(byte)0x0060, (byte)0x0003, (byte)'T', (byte)'h', (byte)'e',
(byte)' ', (byte)'r', (byte)'e', (byte)'q', (byte)'u',
(byte)'e', (byte)'s', (byte)'t', (byte)' ', (byte)'h',
(byte)'a', (byte)'s', (byte)' ', (byte)'t', (byte)'i',
(byte)'m', (byte)'e', (byte)'d', (byte)' ', (byte)'o',
(byte)'u', (byte)'t', (byte)'.', (byte)' ', (byte)'P',
(byte)'l', (byte)'e', (byte)'a', (byte)'s', (byte)'e',
(byte)' ', (byte)'e', (byte)'n', (byte)'t', (byte)'e',
(byte)'r', (byte)' ', (byte)'a', (byte)' ', (byte)'n',
(byte)'e', (byte)'w', (byte)' ', (byte)'U', (byte)'R',
(byte)'L', (byte)'.', (byte)0x0000, (byte)0x0001, (byte)0x0001,
(byte)0x0001
};
return failed;
}
/* Timeout implmented by throwing exception*/
byte b[] = new byte[dgr.getLength()];
b = dgr.getData();
int q = 0;
byte c[] = new byte[1500];
/* Byte 45 is len */
int skip;
skip = ((int)b[3]);
if(skip < 0) {
/* Multi-Byte crap */
byte multiPt1 = b[3];
byte multiPt2 = b[4];
byte finalMult;
int tmp1;
int tmp2;
tmp1 = (int)b[3] + 255;
tmp1 += 1;
tmp2 = (int)multiPt2;
skip = tmp1 + tmp2;
}
skip += 4;
System.arraycopy(b, skip, c, 0, (b.length - (skip)));
/* Code to check for HTTP redirects */
int httpstart = 0;
int semaphore = 0;
int httpend = 0;
int count = 0;
int highpoint = c.length;
int WMLhighpoint = c.length;
boolean NoWML = true;
if(c.length > 255) {
highpoint = 255;
}
if(c.length < 255) {
highpoint = c.length;
}
for(count = 0;count < highpoint;count++) {
if(b[count] == 0xffffff9C) {
if(b[count + 1] == 0x68) {
if(b[count + 2] == 0x74) {
/* We've hit a redirect */
httpstart = count;
semaphore = 1;
break;
}
}
}
}
if(semaphore > 0) {
for(count = httpstart;count < 255;count++) {
if(b[count] == 0x00) {
if(b[count + 1] == 0xffffff9c) {
}
if(b[count + 1] != 0xffffff9C) {
/* This is the end of the redirect */
{
httpend = count;
break;
}
}
}
}
int count2;
String temp;
if(httpstart > 0) {
byte[] newURL = new byte[httpend - httpstart - 1];
System.arraycopy(b, httpstart + 1, newURL, 0, httpend - httpstart - 1);
String bufURL = new String(newURL);
StringBuffer tempURL = new StringBuffer(bufURL);
if(semaphore > 0) {
for(count2 = 0;count2 < tempURL.length();count2++) {
if(tempURL.charAt(count2) == 0x00) {
tempURL.deleteCharAt(count2);
}
if(tempURL.charAt(count2) == '
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -