First I suggest you take a look at http://www.amazon.com/Interfaces-Implementations-Techniques-Creating-Reusable/dp/0201498413 . Secondly, most browsers are asynchronous, so you will need an event library like libuv or libev . In addition, most modern websites require javascript to function properly, but adding a JavaScript browser to your browser will greatly complicate the design. I also do not see mention of how you plan to parse the http sent to and from your browser, I suggest https://github.com/joyent/http-parser .
As for your question about the control flow, I will have a function that parses the response from the server and uses switch() to process the various types of data sent to your browser. There is a field in the http header that explains the type of content, and so your browser should be able to call different functions based on the type of content.
Also look at pointers to functions, how is Polymorphism (in C) here, and here How do pointers to objects in C work? . Function pointers would / could be a more eloquent way to solve your problem, instead using large switch statements through your code. With function pointers, you can have one function that behaves differently when called in your program.
In the following example, I will try to explain the browser.
So, let's say your browser just received an HTTP response from some server. The http response looks something like this: C
struct http_res { struct http_header *header; struct http_body *body int (*decode_body)(char **); };
So first, your http parser will parse the http header and find out if it is a valid answer, and if there is content, etc. etc. If there is content, the parser will check the type and rely on it if it is html, javascript, css or regardless of the fact that the analyzer sets the function pointer to the desired function to decode the http body.
static int decode_javascript(char **body) { return 0; } static int decode_html(char **body) { return 0; } static int decode_css(char **body) { return 0; } int parse_http_header(struct http_res *http) { switch(body_content_type) { case BCT_JAVASCRIPT: http->decode_body = &decode_javascript; break; case BCT_HTML: http->decode_body = &decode_html; break; case BCT_CSS: http->decode_body = &decode_css; break; default: printf("Error can't parse body type.\n"); return -1; } return 0; }
Now, when we pass the HTTP request to another part of the browser, this function can call decode_body() in the HTTP response object, and in the end it will receive a decoded body, which it can understand without knowing what it is decrypting.
int next_function(struct http_res * res) { char *decoded_body; int rtrn; rtrn = res->decode_body(&decoded_body); if(rtrn < 0) { printf("Can't decode body.\n"); return -1; } return 0; }
To make your program truly modular, at least in C , you could use different parts of your browser in different shared libraries, such as an HTTP parser, an event library, a Javascript engine, an html parser, etc. Then you will create interfaces between each library, and you can swap each library with another to change your program, you would link another library at runtime. Take a look at Dr. Robert Martin (Uncle Bob), whom he talks about. This conversation is good, but it lacks slides https://www.youtube.com/watch?v=asLUTiJJqdE , starts at 8:20. This is also interesting, and it has slides: https://www.youtube.com/watch?v=WpkDN78P884 .
And finally, nothing about C , perl or python means you have to transcode your program logic. You will need to develop your program so that each module does not know about each other. The module knows about the interface, and if you connect two modules that "speak" on the same interface, you have created a modular system. Just as the Internet works on different computers on the Internet, you donβt need to know what another computer is or what it does, or its operating system, all they need to know is TCP/IP , and they can communicate with all other devices in the Internet.