Showing posts with label HTML. Show all posts
Showing posts with label HTML. Show all posts

Tuesday, April 9, 2013

HOWTO Not Worry About IE6 Ever Again

While IE6 market share is dwindling, we are far from being out of the woods yet. Gecko, Trident, WebKit and Blink, are likely to never convirge on a full implementation of W3C specifications, leaving some browsers more equal than others.

Not to worry though, there is a nice little script called Modernizr, that can auto-detect features of a browser and automatically load fixes for browsers that don't have the technologies you need.

Shortlist of Features Modernizr Can Detect

  • HTML5 Canvas
  • IndexedDB
  • WebWorkers
  • Lots of CSS3 Stuff
  • Geolocation
  • WebSockets

Thursday, January 24, 2013

HOWTO Setup Automatic Code Hilighting for Websites (and Blogger)

This is actually the method that this blog uses! The scripts below quickly find all code on a page, auto-detect what language it is, and highlight it accordingly.

Setting Up On Blogger

To set up on Blogger all you need to do is the following:
  1. Open the "Template" menu on the left side of your blog's dashboard.
  2. Press the "Edit HTML" button below your template.
  3. Just copy and paste the following code right before the </html> tag.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script src="http://balupton.github.com/jquery-syntaxhighlighter/scripts/jquery.syntaxhighlighter.min.js" type="text/javascript"></script>
<script type="text/javascript">
$('pre').each(function() {
$(this).addClass( "highlight" );
});
$.SyntaxHighlighter.init();
</script>

On Your Website

Alternately, you can add this to your website for automatic code finding and hilighting! Just make sure to add it to the very bottom of your page, or add the third Javascript code block that runs once the page is loaded.

If all of your code is in <code> blocks, just change the pre on line four to code.

Side Note: You probably want to use your own versions of jQuery and jQuerySyntaxHilighter so you can update them yourself.

Wednesday, January 16, 2013

HOWTO Redirect A Page Using Javascript

Occasionally you need to move a page on a website from one location to another, but don't have the ability to update all of the links that point to the old one or access to the server software to do the same thing.

All you need is a page with a little Javascript to solve the problem.

Example

<html>
<head>
<title>Redirecting...</title>
<script type='text/javascript'>
window.location.replace("http://your.site.here");
</script>
</head>
<body>
<p>Redirecting...if you are not redirected click
<a href='http://your.site.here'>here</a>.</p>
</body>
</html>

Notes

  • Include a link and Javascript to redirect for users that have JS turned off.
  • Use window.location.replace instead of window.location.href so the user doesn't get stuck in a loop when they press the back button.
  • Use window.location.replace instead of document.location
  • Insert if(top != self) top.location.replace("http://your.site.here"); in the top of the <script> tag to escape from frames.

Sunday, December 23, 2012

Tiny Embeddable Java Webserver

This is a tiny embedded Java webserver that'll serve pages from a local drive or a .jar (if it is within the jar file).
While it may not be completely useful to do this a very easy extension would include a Map from Strings to Objects, and call the .toString() method on the object and use that as output.
By creating something like this, you would get a simple CGI like server that you could use to write difficult GUI code, using styling from CSS and Javascript.

Source Code

/**
* A local embedded webserver, will run from a .jar file, and serve files
* from the root.
*
* @author Joseph Lewis <joehms22@gmail.com>
*/

import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;


public class Webserver extends Thread
{
private static final FileNotFoundException FILE_NOT_FOUND = new FileNotFoundException();
private static final byte[] ERROR_404 = "HTTP/1.0 404 Not Found\r\nContent-type: text/plain\r\n\r\n404 Error, page not found.".getBytes();
private static final int PORT_NUM = 8_000;
public static Webserver SERVER = null;
private ServerSocket serverSocket;

public static Webserver getInstance()
{
// Make sure we only ever have one server running.
if(SERVER == null)
SERVER = new Webserver();

return SERVER;
}

private Webserver()
{
try
{
serverSocket = new ServerSocket(PORT_NUM);

System.err.println("Starting server on port: " + PORT_NUM);

// If you're running this embedded, you will probably want to
// make the thread a daemon so it quits when your main program
// quits rather than serves forever.
//setDaemon(true);

start();

} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void run()
{
// Starts a new thread for every request we get.
while (true)
try {
new RequestHandler(serverSocket.accept());
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args)
{
Webserver.getInstance();
}


private class RequestHandler extends Thread
{
private final Socket socket;
private final ClassLoader m_loader;
private static final String DEFAULT_INDEX_PAGE = "index.html";

// Start the thread in the constructor
public RequestHandler(Socket s) {
socket = s;
m_loader = Webserver.getInstance().getClass().getClassLoader();
start();
}

@Override
public void run()
{
try
{
DataInputStream in = new DataInputStream(socket.getInputStream());
PrintStream out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()));


// Parse the HTML request
StringTokenizer st = new StringTokenizer(in.readLine());

try
{
String requestType = st.nextToken();
String filename = normalizeFilePath(st.nextToken());

System.err.println("Request: " + requestType + " " + filename);


InputStream f = m_loader.getResourceAsStream(filename);

if(f == null)
throw FILE_NOT_FOUND;

out.print("HTTP/1.0 200 OK\r\nContent-type: " + getMimeType(filename) + "\r\n\r\n");

// Send file contents to client, then close the connection
final byte[] a = new byte[2048];
int n;
while ((n = f.read(a)) > 0)
out.write(a, 0, n);


} catch (NoSuchElementException | FileNotFoundException e) {
out.print(ERROR_404);
}

out.close();
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}

public String getMimeType(String filename)
{
String ext = filename.substring(filename.indexOf(".") + 1);

switch(ext)
{
case "html": case "htm":
return "text/html";
case "css":
return "text/css";
case "java":
return "text/x-java";
case "class":
return "application/x-java";
case "png":
return "image/png";
case "jpg": case "jpeg":
return "image/jpeg";
case "js":
return "application/javascript";
default:
return "application/octet-stream";
}
}

public String normalizeFilePath(String filename) throws FileNotFoundException
{
// Remove GET params.
if(filename.contains("?"))
filename = filename.split("?", 2)[0];

// Make requests to a folder show the index.html file.
if(filename.endsWith("/"))
filename += DEFAULT_INDEX_PAGE;

// Remove leading /
while (filename.indexOf("/") == 0)
filename = filename.substring(1);

// Check for illegal characters to prevent access to superdirectories
if (filename.contains("..") || filename.contains(":") || filename.contains("|"))
throw FILE_NOT_FOUND;

return filename;
}
}
}

Friday, December 2, 2011

Choosing Your Next Webserver

Being an occasional web developer, I like to be able to debug on my local machine. My old friend Apache just wasn't doing it anymore however, because it made my computer boot incredibly slow, I didn't need the power, and the configuration was giving me a headache.

I went in to Synaptic Package Manager and pulled out three others (nginx which I had heard good things about, Cherokee which seemed to have a bunch of packages, and nanoweb a PHP based server that I had never heard of).

Apache
I used gnome-system-monitor for profiling each of the programs as they were running, Apache had 3 threads/child processes running at 7Mb and 4 at 5Mb. That, along with a hefty and often confusing configuration is why I dropped it.

The amount of RAM to beat then was 41 Mb.


Nginx (Engine X)
I first tried a server that I had been dying to give a go for quite some time, Nginx, it runs popular sites such as http://dearblankpleaseblank.com and according to its online site excels at serving static pages.

The configuration didn't really seem like anything I had dealt with before, it was kind of like JSON, kind of like INI and took quite a bit to understand, the online manual wasn't much help and the examples in the file were minimal.

I wasn't able to get PHP running with it even after following a few tutorials. I'm sure you can, but I decided to move on.

The rest of the directory structure was kind of like Apache, with the same sites_available folders and such. However the default site was in /usr/ somewhere rather than /var/www, bizarre.

While it was running it had 5 threads/child processes running at about 2Mb a piece, 10Mb, not bad.


nanoweb
Next up I installed "nanoweb" a simple webserver written in PHP5. After rebooting I found that the default page provided some help on getting things configured, and there was some kind of configuration library that was available as a secondary package, although this would have also been available outside of localhost, and therefore exposed your server to threats.

I took a look at the configuration files and found three, much nicer than Apache. One seemed to be for CGI, I didn't have a reason to look at that, but I was able to see it had support for vhosts (in a very simple ini style file, it looked like each vhost would have taken something like five lines to set up).

I configured my site through the main file replacing all of the default "It Works" style site's values with mine. A reboot worked flawlessly and I was able to start working on my site right away. The file had lots of documentation and tips for configuration.

Using my profiling tool I saw that nanoweb used 3 threds/subprocesses with an average of 7Mb overhead each, undoubtedly due to PHP being a scripted language. 21Mb, not as good as nginx, but not as bad as Apache.

Cherokee
I didn't want to give up the nice nanoweb configuration I had, but I had read some things about it and I wanted to try it.

The install went really fast, unlike nanoweb that took about 20Mb of packages Cherokee must have taken more like four. Probably due to it's base requirements being no more than the C standard library.

I found that to configure you had to run a utility called "cherokee-admin" once run it gives you a username, temporary password, and opens a server on port 9090 until you close the admin interface.

Once logged in the GUI is amazingly nice, giving a nice profile of your machine, easy access to all virtual hosts, and wizards to set up most things. Enabling PHP on my site took four clicks, and it seems that most things are configured for security. Even applying the changes was nearly instantaneous as the web-server can do a restart from the interface.

While running the whole thing has two threads of 2Mb each, 4Mb.

Wednesday, October 19, 2011

Real Time Header Editing with Paros

Sometimes it is advantageous to modify headers as they are sent by a website, let's say you're looking for SQL injection vulnerabilities on your server or want to see what happens when non validated information escapes your javascript validation.

But what to use to easily view/modify/drop headers and incoming connections?

Introducing Paros, a web proxy written entirely in Java so it is cross platform, just set your browser or operating system to use 127.0.0.1:8080 as a proxy after firing it up. Basic functionality shown after the break.





Paros has many advanced features, I'll just discuss trapping requests and responses and using the history.


When you begin using Paros, any websites you visit will be left in the sites pane on the left hand side, from there you can click on each requested item for a site, and view the reqest and response for that item in the appropriate tabs.

If you want to modify the request or response, head over to the Trap tab, and check the appropriate boxes. If there is POST information sent along with the HTTP request, then it will be displayed below; I changed the view to "tabular" to more easily see/edit the data.

Aside: In this instance my browser is pointed at a site called conjuguemos.com teachers us it to test their students' ability to conjugate verbs, when students are done, unencrypted javascript headers are sent containing the session information, not very bright in my book, as any student could quickly and easily modify their scores to have 10,000 words right in thirty-eight seconds.

Once a header/response is trapped you may edit it by hand in the panel given, or drop it (say you wanted to test a timeout for your web widget, like a Twitter reader if Twitter were down).

Sessions can also be saved and restored through the File menu. Overall the tool is easy to use, fast to learn, and very effective.

Tuesday, September 6, 2011

Page Speed Testing

While it is true that the average Internet user is no longer buzzing along at a healthy 56Kbps, that doesn't mean page load speed isn't important. Perhaps your viewers are on a spotty wifi connection, are using a phone to access your site, or really are using a modem.

 You might wan to try out Google's Page Speed lab. It checks for common problems with website page loading speed and gives examples of what can be fixed.

Speedy websites are fun to make, easy to maintain, and helpful to users. If you want to be proactive (before the site is built), here are some tips on how to build site speed in to the development cycle:


Lesson 1: Fun to make
1. Make a small bonfire out of your Dreamweaver, Rapidweaver, Publisher, and and (God forbid) Frontpage discs.
2. Run away before the smoke from burning discs gives you cancer.

3. Now, pull out your favorite text editor and start coding! I like Geany, because it auto-completes HTML tags, highlights CSS, JavaScript, and PHP embedded in HTML and a whole slew of other things, while being lightweight. As a side note, one of my favorite features is Ctrl + Shift + O, which opens the file that your cursor is over, say you had embedded a CSS file, placing the cursor over it and hitting the combo would open it up for editing.

4. Code like the wind!
5. Test.
6. Repeat 4 and 5 until you have the same page you did originally. I usually get the size down by three or four times by hand coding.
7. Run your site through the w3c validators and put a fancy badge on your page.

Where is the fun in this? Well, first of all, you know what every page does and have gained new knowledge in the fields of JS/CSS/HTML, soon enough, you'll be able to re-write entire sites in a few hours for great speed enhancement.

Lesson 2: Easy to maintain
Being your site is now W3C compliant and small, changes are really easy! Want all the text on your site to be purple and flashing? Change it through CSS in thirty seconds. Holiday themes are easy to make now!

What else can you do though? How about separating out the common stuff in each page to a separate php file? And the navigation to a nav generator? Now all of the pages on your site are updated at once when you upload a new one.

This isn't the best practice, but in my opinion, it works for small sites. Why not separate out variables, like phone numbers to a separate php file? Then you can just echo the variable wherever it is needed. Result: Want to change xxx-xxx-yyyy to xxx-xxx-yyyx? You can in a very short amount of time; and you don't have to worry about missing pages!

Lesson 3: Helpful to users
Users that have screen readers installed will thank you, along with users that are on spotty/slow connections. Oh, and your server admins will be happy too, imagine using half the bandwidth through smarter pages?

Saturday, October 23, 2010

Java Source Code Scraping

I am taking a course in Java development whose textbook provides example programs.  Unlike many other books the source code for these programs is trapped inside of HTML pages.  Being a programmer I don't like doing work, and therefore spent some time making a nice Python script that will take in an URL, and folder path, and will scrape the given page for source code, remove all formatting, and output them properly named to the folder specified.



With a little work this script could scrape whatever you wanted, as long as it is between certain tags.

#!/usr/bin/python

import re
import urllib2
import xml.sax.saxutils

numnones = 0

def unescape(string):
'''Unescape the & escapes in html, like &quot; returns a string '''

string = string.replace("&quot;", "\"") #Not done by xml lib
string = xml.sax.saxutils.unescape(string)

return string

def remove_HTML_tags(text):
'''Removes html tags from a supplied string.

Warning: This is accomplished using regular expressions that simply cut
out all text in between and including less than and greater than
characters.

'''

regex_html = re.compile(r'<.*?>')
return regex_html.sub('', text)

def fetch_page(url):
'''Returns the html for the webpage at the supplied url.'''

page = urllib2.urlopen(url)
pagetext = ""

for line in page:
pagetext += line

return pagetext

def get_name(class_text):
''' Returns the name of the abstract, class, or interface, when given the
text of a Java file.

Returns None if the name can not be determined.

Warning: Although Java classes may be named with unicode, this function
will only return characters A-Z, a-z, and 0-9.

'''

#Compile the regular expressions, they all ignore case and accept all chars.
class_ = re.compile(r'public class (.*?) ', re.DOTALL | re.IGNORECASE)
interface = re.compile(r'public interface (.*?) ', re.DOTALL | re.IGNORECASE)
abstract = re.compile(r'public abstract class (.*?) ', re.DOTALL | re.IGNORECASE)

#Find the name of the class/interface/abstract
name = class_.findall(class_text) #Returns [] if none found

if name == []:
name = abstract.findall(class_text)

if name == []:
name = interface.findall(class_text)

#If no name is found return None.
if name == []:
return None

#Remove any remaining non a-z A-Z characters
accepted_chars = re.compile(r'[^A-Za-z0-9]*')
return accepted_chars.sub('', name[0])


def return_between(first_tag, second_tag, text):
'''Returns an array of the text between the delimiters given. All text
between the end and beginning delimiters will be discarded.

Arguments:
first_tag -- The tag to begin text harvesting. (string)
second_tag -- The tag to end text harvesting. (string)
text -- The string in which the tags are to be found. (string)

'''

basic_split = text.split(first_tag)

#select only the sections which contain the close tags, discard the rest.
second_split = []
for i in basic_split:
if second_tag in i:
second_split.append(i)

#Cut out the text before the close tag
between = []

for line in second_split:
value, end = line.split(second_tag, 1)
between.append(value)

return between

def write_file(name, content, location, ext=".java"):
'''Writes a file with the name given, with the content provided, to the
location given, with the given extension.

Arguments:
name -- The name of the file to be written.
content -- The content of the file to be written.
location -- The folder where the file will be created/overwritten.

Keyword Arguments:
ext -- The extension for the file. (default ".java")

Warning: This function will overwrite any pre-existing files without
giving warnings.

'''

if not location.endswith("/"): #FIXME This is not cross platform!
location = location + "/"

print("Location: "+location)

f = open( location + name + ext, "w" )
f.write( content )
f.close()

def main(url, output_folder):
'''Fetches the text at the supplied url and outputs .java files of code
between pre tags.

Arguments:
url -- The url of the page to scrape code from. (string)
output_folder -- The directory that the java files will be created in.

'''
global numnones
page_text = fetch_page(url)

#Get the text between the pre tags
code_and_garbage = return_between("<pre>", "</pre>", page_text)

#Walk through each entry clean it, and write the source file
for i in code_and_garbage:

print i

i = remove_HTML_tags(i)
i = unescape(i)

name = get_name(i)

if name == None:
write_file("None"+str(numnones), i, output_folder)
numnones += 1
else:
write_file(name, i, output_folder)

if __name__ == "__main__":
location = raw_input("Enter the url: ")
output_folder = raw_input("Enter the folder to write to: ")

main( location, output_folder )