/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

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