PageRenderTime 32ms CodeModel.GetById 14ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

/razweb/src/com/razie/pub/comms/UrlComms.scala

http://razpub.googlecode.com/
Scala | 87 lines | 54 code | 17 blank | 16 comment | 9 complexity | 846fb7403da9a70814a7dcbeab4fb4c5 MD5 | raw file
 1package com.razie.pub.comms
 2
 3import razie.base._
 4import com.razie.pub.base._
 5import java.net._
 6import java.io.BufferedReader;
 7import java.io.File;
 8import java.io.IOException;
 9import java.io.InputStream;
10import java.io.InputStreamReader;
11import java.io.OutputStream;
12import java.io.OutputStreamWriter;
13import java.net.MalformedURLException;
14import java.net.Socket;
15import java.net.URL;
16import java.net.URLConnection;
17import java.util.List;
18
19import org.xml.sax.InputSource;
20import org.xml.sax.SAXException;
21
22//import razie.assets.AssetLocation;
23import razie.base.AttrAccess;
24//import com.razie.pub.base.data.ByteArray;
25//import com.razie.pub.base.exceptions.CommRtException;
26import com.razie.pub.base.log.Log;
27import com.razie.pubstage.comms._
28import com.sun.org.apache.xerces.internal.parsers.DOMParser;
29
30class UrlComms {
31
32}
33
34class UrlConn (val baseUrl:String) {
35   val url = new URL (baseUrl)
36   val socket = new Socket (url.getHost, url.getPort)
37 
38   /** @param path does not include server:port */
39   def get (path:String, httpArgs : razie.AA = razie.AA()) : String = {
40      HttpHelper.sendGET (socket, "GET "+path+" HTTP/1.1", httpArgs)
41            var input = "";
42
43                // don't know why but i can't use this compliant reader...
44                // BufferedReader in = new BufferedReader(new
45                // InputStreamReader(socket.getInputStream()));
46
47                // Get input from the client
48                val in = new java.io.DataInputStream(socket.getInputStream());
49
50                // TODO 3 why do i flush empty lines?
51                // consume the input until a non-emtpty line
52            while (input == null || input.length() <= 0) {
53               input = in.readLine();
54               if (input == null) {
55//                  logger.alarm("ERR_SOCKET_EOF socket had EOF before any byte...dropping connection");
56                  return "";
57               }
58            }
59
60                 var rest = in.readLine();
61                val httpattrs = new AttrAccessImpl();
62                while (rest != null && rest.length() > 0) {
63                   val s = rest.split(": ");
64                   httpattrs.set(s(0), s(1));
65                    rest = in.readLine();
66                }
67
68      val contents = Comms.readStreamNoClose (in)                
69                
70      if (input.endsWith("200 OK")) {
71         var es = 0
72//         val contents = new StringBuffer()
73//         for (s <- 1 until lines.length)
74//            if (lines(s).length <= 0 && es <= 0)
75//               es = s
76//            else if (es > 0) contents.append(lines(s)).append("\n")
77            
78         val c = (contents.toString)
79         HtmlContents.justBody (c)
80      } else
81         input
82   }
83   
84   def close = socket.close
85}
86
87