RSS
 

Archive for the ‘Technical’ Category

HTTP via Telnet

02 May

Okay, back to basics, the very low level basics. HTTP is text based, telnet enables a network connection via a text-based command line. All good. We can use telnet to talk to a web server by manually typing text to it, if you know the details of the content syntax to send HTTP via telnet.

Though this is not done, often, it is still good to know how it can be done. In fact, since these basics rarely change, I shouldn’t have even had to write this down, since it should be innate.

Sending HTTP Using Telnet

Once you get telnet running (see section, below), it is just a matter of typing HTTP to the server, manually.

There are four pieces the first three are shown, here:

GET / HTTP/1.1
Host: cachecrew.com
Connection: close
 
  1. This is the start of the HTTP commands you can enter. The first line must specify the HTTP method.  There are three parts:
    1. HTTP method: GET, POST, PUT, DELETE, etc.
    2. The URI path; the path portion starting with ‘/’, which excludes the server portion.
    3. Protocol: HTTP/1.0 or HTTP/1.1. If you specify HTTP/1.1, you should probably specify Connection: close header otherwise the server will timeout and return an extraneous error (because HTTP version 1.1 defaults to a “persistent” connection).
  2. HTTP headers
  3. Empty line. This marks the end of the HTTP headers.
  4. If a Content-Length header was included, then content follows the empty-line marker as the fourth piece. (Not shown in the first example)

Important HTTP Header Notes

  1. Any errors made during input will often invalidate the entire entry. Typing and backspacing to correct will send the backspace characters to the server; this is not part of the HTTP syntax and will probably yield an error.
  2. A Host header should be sent. Some servers may host multiple domains on the same server-node and the host name is the only way those machines can differentiate between the target servers.
  3. As mentioned, if the protocol line (5) specifies the 1.1 version of HTTP, then the server will maintain an open connection, but time out with an error if further input is not sent. When specifying 1.1, it’s best to include a Connection: close header to tell the server not to keep the connection open.
  4. If you are sending “content”, you must specify a Content-Length header, specifying the number of bytes contained in the content. This value will truncate any content exceeding this length and it will cause the server to wait for additional input if the number is too big.

The server will only recognize “abcdefghijklmnopqrstuvwxyz”, even though a newline and “0123456789” were sent.

⋮
Content-Length: 26

abcdefghijklmnopqrstuvwxyz
0123456789

Notice that the line breaks are usually two-bytes, carriage-control/linefeed (0x0D, 0X0A), so

⋮
Content-Length: 28

abcdefghijklmnopqrstuvwxyz
0123456789

will be read as “abcdefghijklmnopqrstuvwxyz”[CR,LF].

⋮
Content-Length: 38

abcdefghijklmnopqrstuvwxyz
0123456789

will read the entire content “abcdefghijklmnopqrstuvwxyz”[CR,LF]”0123456789”.

The first lines of telnet are excluded, here, but shown in the next section.

The Telnet Command

This is one of the simplest network commands around. It connects the command like (that is the keyboard and screen) to a server, to send text (mostly) back and forth. You type, and the server responds. Its mechanism for communicating is dead-simple and can be re-purposed to communicate with other text-based services.

In the world of the Internet, in addition to knowing the name/address of a server, connecting to a service on the server is simply a matter of knowing the port to connect on. Telnet defaults to its port 23, which is not likely to work on a public server (or shouldn’t, if the server owner knows what is good for them). Web servers talk to web pages over port 80, by default. We can use telnet to talk with a web server by specifying port 80:

$ telnet cachecrew.com 80
Trying 208.113.163.125...
Connected to cachecrew.com.
Escape character is '^]'.
GET / HTTP/1.0
Host: cachecrew.com
 

The first and other highlighted lines are what you type.

  1. Use the telnet command to connect to the default web servers (port 80) at the cachecrew.com server.
  2. Telnet resolves the IP address from the server name and attempts to connect.
  3. Now it’s connected.
  4. You can type ctrl-] to get to telnet’s command mode. Then, if you want to quit, type “quit”. “Help” will list telnet commands you can use.
  5. This is the start of the HTTP commands you can enter. This is detailed in the section above.

On MacOS and Linux, telnet should already be installed and available from a terminal-command-line. On Windows, it is part of an installation feature which can be installed, explicitly, see How to Install Telnet in Windows 10. If you want Linux-like command-line features one Windows, you can install Cygwin. This can make telnet available to Windows’s regular cmd command-line, as well.

Telnet was originally used as a primitive way to communicate with a server. Because it lacks any decent security, servers rarely expose a default service for telnet to access. And, today, as most web servers enforce access via https, only, telnet is becoming less and less useful for accessing other commercial servers. For development, debugging, or testing a server which you can control, however, this can still be a useful tool.

 
 

Promises, Promises… Timeout!

25 Apr

I was playing with ES6’s new Promises, for the first time, this week. Then, I was looking at the ugliness of using a browser’s setTimeout() function and thought that it would look better as a Promise.

tl;dr summary: A Simple Promise Version of “setTimeout()”

If we do it right, you simply specify the timeout period and implement a handler for then() to invoke:

timeout(5000) // Delay for 5000 ms
   .then(function () {
      // Do, here, whatever should happen when the time has elapsed…
   });

Or, since this is ES6, we might as well use arrow-function shorthand:

timeout(5000).then( () => {
   // do your thing, here…
})

Implementation

The setTimeout() already functions a bit like a Promise, without matching the promise-pattern and syntax, so converting it to a Promise is pretty easy:

function timeout(delay) {
   return new Promise( 
      (resolve, reject) => setTimeout( resolve, delay )
   )
} // timeout()

This all, in contrast to the “old” way of using setTimeout():

setTimeout(function () {
   // This is where I do my thing, when the timer pops…
}, 5000);

It just looks ugly; the invocation of the timer and its delay-time property is mingled with what happens when the timer triggers. With the promise pattern, the semantics of the invocation and the handling are discretely organized between the timeout() and its then() handler. Read the rest of this entry »

 
 

Simplicity via PHP Frameworks

05 Apr

PHP language and web-technologyA long time ago, I’d endeavored to create my own lightweight PHP framework, ccPHP. It was a good exercise, but never reached prime time. Around that time I began to notice others endeavoring to do the same thing (i.e., create simpler, lighter weight PHP frameworks that didn’t take more time to learn than to implement the project). I’ve been away for a while and things have changed.

  1. The continued refinement and simplification of frameworks.
  2. The greater emphasis on frontend apps to do more of the work that used to be done in the middle tier.
  3. The ubiquity of RESTful interfaces, the dividing line between front-ends and servers.

Micro-PHP Frameworks

I doubt that I will ever finish my own framework, now that others are a lot closer to the goals that I was moving towards. And, right now, I simply want to find a lightweight modular frameworks that truly help me get my core work done. Dragan Gaic did a great survey of the PHP hot frameworks in Top 12 Best PHP RESTful Micro Frameworks (Pro/Con).

A key feature of such frameworks is for them to be modular to allow me to pick and choose only the components I need, rather than have to install, understand, and run a bunch of functionality that I do not need. It makes it harder for the developer and impacts performance of the results. Model-View-Controler (MVC) is a common software architecture pattern. With the client side being far more capable of managing the view part, on its own, the server side can be simpler. Instead of cluttering up the server-side app with trying to display information, it can simply focus on securing data and access and providing data services via RESTful interfaces.

So, what I am looking for are pieces to help me access persistent data and build RESTful interfaces. And something to help keep all the code flow organized. Based on the article, I can look at Silex, Slim, Lumen, or Phalcon for the latter. For database access, Ive worked with RedBeans before and liked it, so I will probably pick that up again. If one of the frameworks doesnt help implement and configure a RESTful interface simply, then I may get back into the framework building business again.

Model: RedBeans
View: Frontend client
Controler: one of SilexSlimLumen, or Phalcon

A WordPress bug allowed my website to be hacked. I thought this was lost forever until I thought to reconstitute it from http://Archive.org!!

 

Solving git Problems with Prune

28 Jan

gitThe more I use git, the more I find that it is not as bullet-proof as I have been led to believe. Though, so far, there seems to always be a way out of a problem. Such is the case when I was unable to update a local branch from our

Your configuration specifies to merge with the ref 'my_branch'
from the remote, but no such ref was fetched.

No matter what I did to recreate my local branch, I could not pull from the remote. Google and StackOverflow was of no help—misdirecting to unhelpful information. It turned out that even through I’d fetched the current state of the remote server, my index was still showing a remote branch that did not really exist. Running the following command cleaned that up—and all was well:

$ git remote prune origin
Pruning origin
URL: git@github.….com:….git
 * [pruned] origin/my_branch
$ _

For some reason there was a phantom remote branch that fetch did not update in my local index, properly. Otherwise prune is pretty innocuous, so it might be worth running, every once-in-a-while to make sure that your index is an accurate reflection of the remote.

Enhanced by Zemanta
 
 

git “Getting Started” Tips

21 Jan

git is has become one of the most popular source code control tools on the planet. Even if you’re coming from another source control system, becoming proficient with git can take some time. The best place to start is by running through the git Tutorial. Before that, you might take a look at the GitGuys article to get a quick synopsis of git concepts. Also, though slightly out of date, keeping the following diagram in front of you will help as well. It shows the git command-line commands that “move” file changes from one place to another in the git data-store hierarchy. Read the rest of this entry »

 
 

Activating OS X’s Builtin Apache Web Server and PHP

14 Jan

As I mentioned in my prior post OS X Command-line Dev Tools—Install Shortcut, OS X comes with a variety of built-in tools suited for developers. Unfortunately, Apple has seen fit to hide one of the most important of them: the web server. Despite that, they are easily unlocked if you know how. This applies to Mountain Lion (10.8) and (though I haven’t tried it, yet) Mavericks (10.9). Though you can install separate installations, I don’t like to install anything extra, unless I must. Read the rest of this entry »

 

Command line clipboard in OSX and Cygwin

03 Dec

I use the command line a lot, even though I am on a graphical user-interface like Windows or OSX. And since I’m lazy, I write a lot of scripts to perform repetitive tasks. The downside of command-line is that there is no standard way to interact with GUI features. I stumbled across a command in OSX, recently that allows command line programs to copy/paste between the clipboard that we’re so used to using.

OSX pbpaste and pbcopy

OSX has two commands, pbpaste and pbcopy, Read the rest of this entry »

 

Syncing Github Forks

26 Nov

GitHub_LogoGithub has become the place to manage source code—it’s free, robust, and commonly accessible. Github repositories are owned and access to change them must be enabled by the owner. Still, you can make changes to a Github repository; but a “pull request” must be sent to contributors of the repository and they can accept the change at their discretion. Keeping changes in sync when you don’t have direct change privileges may not be obvious.

One-time Github Changes Read the rest of this entry »

 

PHP Recipe to Show Source-code

19 Nov

PHP programming referenceHere is a simple Web 1.5 (static HTML with a little bit of styling and JavaScript) recipe to allow a viewer of your web page to see the PHP source code of the page with a minimal amount of JavaScript and a little CSS manipulation—good for showing the work you’ve done to others. Or for embedding in your own source, in debug mode, so that teammates can see each others’ work.

See the example code at: http://cachecrew.com/sample_code/highlight_file.php and http://cachecrew.com//sample_code/highlight_file2.php.

The PHP and HTML Read the rest of this entry »

 

OS X Command-line Dev Tools—Install Shortcut

12 Nov

Xcode command-line toolsAmong those around me, in world of tech startups (even in the vicinity of the world of Windows), MacBooks are used almost universally. I can’t explain the discrepancy between what I see around me and the data you usually read; but I do know that as a technical platform, OS X provides an easier path to development tools. These days, the world driven by web-based applications. A majority of those applications run on Linux-based machines. OS X shares with Linux, a common ancestor, Unix. So, a robust array of common commands come with OS X—ssh, ftp, bash, vi, emacs, etc. But more importantly, OS X comes pre-installed with hottest development tools used for web development, today—Ruby, Python, PHP, Apache, memcache, etc. This means a developer can write web applications without even being connected to the Internet!

There are more tools available with the free download of Xcode, Read the rest of this entry »