jynx.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * Jynx
  3. *
  4. * The Java-based Lynx clone.
  5. * -----------------------------------------------------------------------------
  6. * Programmer: Jonathan Landrum
  7. * Program: jynx.java
  8. * Date: 23 February 2013
  9. * Purpose: This program attempts to be a text-based browser in the
  10. * fashion of Lynx. It is so named because it is "Jon's Lynx",
  11. * or alternatively, "Java Lynx".
  12. * Dependencies: java.io.*
  13. * java.net.*
  14. * java.util.*
  15. * Changelog:
  16. * -----------------------------------------------------------------------------
  17. * Date: Programmer: Version: Description:
  18. * -----------------------------------------------------------------------------
  19. * 25 Jul 14 jonlandrum 1.0 Cleaned up output, solved redirects
  20. * 22 Jul 14 jonlandrum 0.1.1 Moved to GitHub
  21. * 23 Feb 13 jonlandrum 0.1 Initial release
  22. * -----------------------------------------------------------------------------
  23. */
  24. import java.awt.*;
  25. import java.awt.event.*;
  26. import java.io.*;
  27. import java.net.*;
  28. import java.util.*;
  29. import javax.swing.*;
  30. public class jynx {
  31. public static void main (String[] args) throws IOException {
  32. // Create a scanner object and input variable
  33. String input = "";
  34. Scanner scan = new Scanner(System.in);
  35. // Open the containing frame
  36. //javax.swing.SwingUtilities.invokeLater(new Runnable() {
  37. //public void run() {
  38. //createAndShowGUI();
  39. //}
  40. //});
  41. // Grab a URL from the user
  42. System.out.println ("Jynx, the Java-based Lynx clone.");
  43. System.out.print ("Address: ");
  44. input = scan.nextLine();
  45. // Attempt a connection with an initial redirect count of 0
  46. open(input, 0);
  47. }
  48. public static void open (String input, int limit) throws IOException {
  49. String output = "", host = "", port = "", directory = "";
  50. int portNum = 80, redirectLimit = limit;
  51. Socket socket = null;
  52. PrintWriter out = null;
  53. BufferedReader in = null;
  54. // Assume http(s); don't want to go down the gopher road
  55. if ((input.length() > 7) && (input.substring(0, 7).equals("http://"))) {
  56. input = input.substring(7);
  57. } else if ((input.length() > 8) && (input.substring(0, 8).equals("https://"))) {
  58. input = input.substring(8);
  59. portNum = 443;
  60. }
  61. // Grab the host and directory
  62. for (int c = 0; c < input.length(); ++c) {
  63. if (input.charAt(c) == '/') {
  64. host = input.substring(0, c);
  65. directory = input.substring(c);
  66. break;
  67. }
  68. }
  69. // Ensure we have a host and a directory (happens when no file or directory are specified in the request)
  70. if (host.equals("")) {
  71. host = input;
  72. directory = "/";
  73. }
  74. // Check for a specified port
  75. for (int c = 0; c < host.length(); ++c) {
  76. if (host.charAt(c) == ':') {
  77. for (int i = c + 1; i < host.length(); ++i)
  78. port += host.charAt(i);
  79. portNum = Integer.parseInt(port);
  80. host = host.substring(0, c);
  81. break;
  82. }
  83. }
  84. try {
  85. // Set up the socket
  86. socket = new Socket (host, portNum);
  87. out = new PrintWriter (socket.getOutputStream());
  88. in = new BufferedReader (new InputStreamReader (socket.getInputStream()));
  89. // Send the request and get the response
  90. out.print("GET " + directory + " HTTP/1.1\r\n");
  91. out.print("Host: " + host + "\r\n");
  92. out.print("\r\n");
  93. out.flush();
  94. // Check for 301 or 302 redirects
  95. while (!(output = in.readLine()).toLowerCase().contains("<body")) {
  96. if ((output.toLowerCase().contains("301 moved permanently")) ||
  97. (output.toLowerCase().contains("302 found"))) {
  98. while (!(output = in.readLine()).toLowerCase().startsWith("location: "));
  99. host = output.substring(10);
  100. // Close all connections
  101. in.close();
  102. out.close();
  103. socket.close();
  104. // Start a new connection
  105. if (redirectLimit < 10) {
  106. open(host, ++redirectLimit);
  107. } else {
  108. System.out.println ("Error: Too many redirects.");
  109. }
  110. }
  111. }
  112. // Print the body text (assume we're hitting a site with valid html)
  113. while (!(output = in.readLine()).toLowerCase().contains("</body")) {
  114. System.out.println (output);
  115. }
  116. } catch (Exception e) {
  117. // Handle any exceptions
  118. System.err.println (e);
  119. System.exit(1);
  120. } finally {
  121. // Close all connections
  122. in.close();
  123. out.close();
  124. socket.close();
  125. System.exit(0);
  126. }
  127. }
  128. private static void createAndShowGUI() {
  129. //JFrame.setDefaultLookAndFeelDecorated(true);
  130. JFrame frame = new JFrame("Jynx");
  131. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  132. JLabel emptyLabel = new JLabel("");
  133. emptyLabel.setPreferredSize(new Dimension(800, 600));
  134. frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
  135. frame.pack();
  136. frame.setVisible(true);
  137. }
  138. }