Skip to content

On Being a Web Client

August 25, 2015

It’s a bit counter-intuitive but being a web server is quite a bit easier than being a client.  The client is usually a pretty sophisticated browser running on a capable machine so a lot is expected of it.  The server can get away with spewing out some fixed html but the client has to parse and deal with it.

My servers usually open a socket and set it to listen on port 80, when a session is established they slurp in the request, pay not too much attention to it, and spit out a response.

To be a client, you first have to find the ip of your target with dns, then you have to issue a connect and check that it works, then send your request, wait for a response, and deal with that.  You can also get redirection and any number of other complexities added in.

To simplify things, I’m trying to work with an ip address that i’ve looked up in advance by pinging it.  www.google.ca pings to 24.156.153.25 so i’m bolting that into a variant of my server program and trying to connect rather than listen.  After a bunch of false starts I got google to respond with 2048 bytes of gobbledygook so that’s a start.

The status code 15 means it’s in connection state equivalent to 14 for listen I guess. Code 17 is session established as usual. Once I got code 17 i just sent my request and started polling for available data. Quite quickly I got 2048 bytes back which was all i was set up to handle. I’m sure there’s some organized way of getting the next window of data. My goal though is to hit a twitter server so i’m hoping i don’t get that much back.


/*****************************************************************************
//  File Name    : w5500mule.c
//  Version      : 1
//  Description  : wiznet w5500 test harness
//  Author       : WJR with thanks Karl Lunt & Wiznet
//  Target       : Olduino
//  Compiler     : LCC1802
//  IDE          : TextPad
//  Programmer   : Olduino bootloader via avrdude
//  Created		 : Nov 26, 2014
*****************************************************************************/
#define	nofloats			//not using floating point
#include <nstdlib.h> //for printf etc.
#include <cpu1802spd4port7.h>
#include <olduino.h> //for digitalRead, digitalWrite, delay
#include <hspi2.h> //hardware spi header
#include "w5500data.h"  //wiznet definitions
#include "w5500code.h"  //wiznet code definitions
union IPaddr thisip={182}; //the ip that a form/request came from
#define MAX_BUF 1024
unsigned char buf[MAX_BUF];			//memory buffer for incoming & outgoing data

void sendreq(){//send a request
	printf("getting response\n");
    sendlit("GET /search?q=olduino HTTP/1.1\n");
    sendlit("Host: www.google.ca\n");
    sendlit("Connection: close\n");
    sendlit("\n");
}
void getresp(){	//handle a session once it's established
	unsigned int rsize,strncmpval;
	unsigned int tries=100;
	printf("getting response\n");
	rsize=wizGetCtl16(SnRX_RSR); //get the size of the received data
	while(rsize==0 && tries-->0){
		delay(20);
		printf("re-size ");
		rsize=wizGetCtl16(SnRX_RSR); //retry size of the received data
	}
	printf("**rsz=%d\n",rsize);
	if (rsize>0){
		thisip.l=getip();
		if (recv0(buf,min(1023,rsize))>0){ //get some characters
			printf("%s\n",buf);
		}
	}
	printf("flushing %d\n",rsize);
  	if (rsize>0) flush(rsize);	//get rid of the received data
	wizCmd(CR_DISCON);// Disconnect the connection- we're done here
	printf("done\n>\n");
}


void wizWrite(unsigned int addr, unsigned char opcode, void * data, unsigned int len);
void socket0_client_init(){ //initialize socket 0 for http client
    unsigned char ggl_addr[]={24,156,153,25};//address for google destination
	wizCmd(CR_CLOSE); //make sure port is closed
	wizSetCtl8(SnIR,0xFF); //reset interrupt reg
	wizSetCtl8(SnMR,MR_TCP); //set mode register to tcp
	wizSetCtl16(SnPORT,1024); //set tcp port to 80
   	wizWrite(SnDIPR,WIZNET_WRITE_S0R,ggl_addr, 4); //write the outgoing dest ip address
   	wizSetCtl16(SnDPORT,80);//destination port
	wizCmd(CR_OPEN); //open the port
	wizCmd(CR_CONNECT); //try to make a conection
}

void main(void){
	unsigned char socket0status;
    unsigned char ip_addr[] = {192,168,1,182}; //{169,254,180,2}; //
	delay(100);
	printf("\nW5500 Test Mule Client\n");
	delay(500);
    wiz_Init(ip_addr); //initialize the wiznet chip
	socket0_client_init();

	while(1){  // Loop forever
		socket0status=wizGetCtl8(SnSR);
		printf("s0s=%cx ",socket0status);
		if (socket0status==SOCK_ESTABLISHED){ //we're connected
			sendreq();
			getresp();
			printf("stalling\n");
			while(1);//loop here
		}
		delay(100);
	}
}
#include <olduino.c>
#include <nstdlib.c>
#include <hspi2.c>
#include "w5500code.c"

 

13:14:51.777> W5500 Test Mule Client
13:14:52.399> Done Wiznet W5500 Initialization on IP address 192.168.1.182
13:14:52.399> Gateway Address 192.168.1.1
13:14:52.399> 
13:14:52.711> s0s=15 s0s=15 s0s=17 getting response
13:14:52.711> getting response
13:14:52.836> re-size re-size re-size re-size **rsz=2048
13:14:52.898> HTTP/1.1 200 OK
13:14:52.898> 
13:14:52.898> Date: Tue, 25 Aug 2015 17:14:52 GMT
13:14:52.898> 
13:14:52.898> Expires: -1
13:14:52.898> 
13:14:52.898> Cache-Control: private, max-age=0
13:14:52.898> 
13:14:52.898> Content-Type: text/html; charset=ISO-8859-1
13:14:52.898> 
13:14:52.962> P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
13:14:52.962> 
13:14:52.962> Server: gws
13:14:52.962> 
13:14:52.962> X-XSS-Protection: 1; mode=block
13:14:52.962> 
13:14:52.962> X-Frame-Options: SAMEORIGIN
13:14:52.962> 
13:14:53.024> Set-Cookie: PREF=ID=1111111111111111:FF=0:TM=1440522892:LM=1440522892:V=1:S=bPgUObdOy_1YCzrj; expires=Thu, 31-Dec-2015 16:02:17 GMT; path=/; domain=.google.ca
13:14:53.024> 
Set-Cookie: NID=70=JE6B7I2GKHSsE6xgpnZXSWt0-IS-Ww2BOKj8BNsJysk7nm2MzFYMW4Csmp_q70-oChJBLK3_v-13:14:53.086> gEbWbfQF4velwKkswhCYo2gWR7zezRcZ9q0BldXAQGzdSh2vN_0KPP; expires=Wed, 24-Feb-2016 17:14:52 GMT; path=/; domain=.google.ca; HttpOnly
13:14:53.086> 
13:14:53.086> Accept-Ranges: none
13:14:53.086> 
13:14:53.086> Vary: Accept-Encoding
13:14:53.086> 
13:14:53.086> Connection: close
13:14:53.086> 
13:14:53.086> 
13:14:53.086> 
<!doctype html><html itemscope="" 13:14:53.148> itemtype="http://schema.org/SearchResultsPage" lang="en-CA"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/google_favicon_128.png" itemprop
13:14:53.148> flushing 2048
13:14:53.148> done
13:14:53.148> >
13:14:53.148> stalling
Advertisements

From → web server

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: