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.
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.
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:
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.
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:
- The event's character code is 'p', which invokes a HandlePost() function.
- After entering the event code, the user enters 3 numbers:
- User ID
- Passcode
- New default page number
- If the user ID and passcode match the stored user ID and passcode, the new default page number gets passed back to main() for future storage.
- For logging purposes, the HandlePost() function returns TRUE (1) for "success" and FALSE (0) for failure (invalid passcode or ID).
Consider (a) how you need to change main() and (b) what parameters the event handler needs.
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:
- If the user enters -1 as the page code, assume (s)he is requesting the default page. In that case, take 2 more numbers from the user: user ID and passcode. If they match the stored user ID and passcode, save them.
Consider how you will have to change HandleGet(): does it now require parameters? Do you really need pointers or not?
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.
- 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.