<-- back to NAW index...

CSE 142 : The NAW Server Session 2

The story so far:

Last week, we implemented a simple event loop that responded to GET requests from the user. I've taken student code, cleaned it up a bit, and added infrastructure to make it a full program; see the other handout.

Solutions and notes...

Plan:

In this session, we're going to add some simple authentication abilities to the server, so a single user can store some simple data. Next session, we'll expand authentication to allow multiple users.

A scheme for defining users

Since we don't know how to handle strings yet, we'll identify users by a unique ID number, and also use numbers as passcodes.

Now we need a command to add a single user ID and passcode. This is accomplished by adding a new event type; we'll use the character code 'a' for "add". Observe that:

  1. We can't make ID and passcode local variables of our event handler function, because then they'll disappear when the function returns.
  2. We need to pass two distinct pieces of data back to main(): the ID, and the passcode. So we can't just use the return value.

Therefore, we need pointer parameters.

Exercise One

Write a function HandleAdd() to handle the "add user" event. It should take two output parameters, read two numbers from standard input (user ID and passcode), and pass them back to main(). We probably also want to notify the user of success by printing some output.

Then, modify main() to accept this event type.

Sample solution

Using authentication to allow personalized responses

Now that we can save a user, let's add the POST protocol so that we can store a personalized "preference". One simple preference is a default page number to print, when no other page is specified.

So, let's add a way of POSTing the home page number:

Exercise Two

Add a "POST" event. The POST event is specified as follows:

Consider (a) how you need to change main() and (b) what parameters the event handler needs.

Sample solution

Creating a personalized response

Finally, let's modify the GET protocol so that it allows us to provide the user with his/her default preferred page:

Exercise Three

Modify the GET protocol as follows:

Consider how you will have to change HandleGet(): does it now require parameters? Do you really need pointers or not?

Sample solution

That's enough for today. In fact, we probably won't/didn't even finish all of the above today, in which case I urge you to do the last exercise yourself.


Thoughts to take home with you:

- Don't use pointer parameters unless necessary.

- When you need pointer parameters, be very careful about using & and *. Leaving them out is a very common error.

- Consider the way we've built up this project so far: we started with a very small, working core, made of very simple parts. Then, we progressively added to that core little by little, building outwards. This model is called "spiral development", because during development the program spirals outward into something larger, like a nautilus shell. Try to use a spiral development model in your own code: as projects get larger, you will see gains from this development model.


Keunwoo Lee
Last modified: Fri Nov 5 15:55:06 PST 1999