| worker MPM | prefork MPM | |
|---|---|---|
How it works | Uses multiple child processes. It's multi-threaded within each child, and each thread handles a single connection. | Uses multiple child processes, each child handles one connection at a time. |
Fast | fast and highly scalable | speed is comparable to that of worker |
Memory | Memory footprint is comparatively low | Memory usage is high, and more traffic leads to greater memory usage |
Stability | less tolerant of faulty modules, and a faulty thread can affect all the threads in a child process. | highly tolerant of faulty modules and crashing children |
Suited for | multiple processors | single or double CPU systems |
Thursday, November 15, 2007
Apache - Multi-Processing Modules (MPMs)
MPMs are Responsible for binding to network ports on the machine, accepting requests, and dispatching children to handle the requests. For Linux, two types: worker (threaded MPM) and prefork (non-threaded MPM)
Tuesday, July 03, 2007
Better Code Through Destruction
Perl's garbage collector counts references. When the count reaches zero (which means that no one has a reference), Perl reclaims the entity. The approach is simple and effective. However, circular references (when object A has a reference to object B, and object B has a reference to object A) present a problem.
A common example is a tree-like data structure. To navigate both directions--from root to leaves and vice versa--a parent node has a list of children and a child node has a reference to its parent. Many CPAN modules implement their data models this way, including HTML::Tree, XML::DOM, and Text::PDF::File. All these modules provide a method to release the memory. The client application must call the method when it no longer needs an object. However, the requirement of an explicit call is not very appealing and can result in unsafe code.
Solution:
Instead of explicitly calling the method to delete the object, create a special guard object (of another class) whose sole responsibility is to release the resource. When the guard object gets destroyed, its destructor deletes the tree.
Note that now there is no need to call $tree->delete explicitly at the end of the loop. The magic is simple. The code of DESTROY method of the Sentry package calls, in turn, the method delete of the $tree object.
Finally, there is no need to code your own Sentry class. Use Object::Destroyer, originally written by Adam Kennedy.
Source: http://www.perl.com/pub/a/2007/06/07/better-code-through-destruction.html
A common example is a tree-like data structure. To navigate both directions--from root to leaves and vice versa--a parent node has a list of children and a child node has a reference to its parent. Many CPAN modules implement their data models this way, including HTML::Tree, XML::DOM, and Text::PDF::File. All these modules provide a method to release the memory. The client application must call the method when it no longer needs an object. However, the requirement of an explicit call is not very appealing and can result in unsafe code.
Solution:
Instead of explicitly calling the method to delete the object, create a special guard object (of another class) whose sole responsibility is to release the resource. When the guard object gets destroyed, its destructor deletes the tree.
use HTML::TreeBuilder;
foreach my $filename (@ARGV) {
my $tree = HTML::TreeBuilder->new;
$tree->parse_file($filename);
my $sentry = Sentry->new($tree);
next unless $tree->look_down('_tag', 'img');
## next, last or return are safe here.
## Tree will be deleted automatically.
}
package Sentry;
sub new {
my $class = shift;
my $tree = shift;
return bless {tree => $tree}, $class;
}
sub DESTROY {
my $self = shift;
$self->{tree}->delete;
}
Note that now there is no need to call $tree->delete explicitly at the end of the loop. The magic is simple. The code of DESTROY method of the Sentry package calls, in turn, the method delete of the $tree object.
Finally, there is no need to code your own Sentry class. Use Object::Destroyer, originally written by Adam Kennedy.
Source: http://www.perl.com/pub/a/2007/06/07/better-code-through-destruction.html
Thursday, June 21, 2007
Switching between HTTP and HTTPS
Question:
Following statements are there in my Apache2 virtual hosts section
Redirect /host/directory/folder/login.php https://servername/host/directory/folder/login.php
Redirect /host/directory/folder/register.php https://servername/host/directory/folder/register.php
How to return to http (not https) when the above 2 pages are not involved?
Answer:
In HTTP VH:
RewriteRule /.../login.php https://.../login.php [R]
RewriteRule /.../register.php https://.../register.php [R]
In HTTPS VH:
RewriteRule /.../login.php - [S=2]
RewriteRule /.../register.php - [S=1]
RewriteRule ^/(.*) http://servername/$1 [R]
In the HTTPS VH, if you get login.php or register.php you don't rewrite it (the "-"), then you skip the next 2 or 1 RewriteRules ( [S=2], [S=1]) - that is, you skip over the general purpose rewrite back to HTTP. So login.php or register.php get served from HTTPS and everything else goes back to HTTP.
Following statements are there in my Apache2 virtual hosts section
Redirect /host/directory/folder/login.php https://servername/host/directory/folder/login.php
Redirect /host/directory/folder/register.php https://servername/host/directory/folder/register.php
How to return to http (not https) when the above 2 pages are not involved?
Answer:
In HTTP VH:
RewriteRule /.../login.php https://.../login.php [R]
RewriteRule /.../register.php https://.../register.php [R]
In HTTPS VH:
RewriteRule /.../login.php - [S=2]
RewriteRule /.../register.php - [S=1]
RewriteRule ^/(.*) http://servername/$1 [R]
In the HTTPS VH, if you get login.php or register.php you don't rewrite it (the "-"), then you skip the next 2 or 1 RewriteRules ( [S=2], [S=1]) - that is, you skip over the general purpose rewrite back to HTTP. So login.php or register.php get served from HTTPS and everything else goes back to HTTP.
Thursday, June 07, 2007
Perl Code Profiling - Apache::DProf
- In Startup.pl:
# The Apache::DProf requires this
use Apache::Registry; - In conf:
PerlModule Apache::DProf
- Make sure the logs directory has write rights
- Restart Apache & do whatever you want in the browser page
- Stop Apache (only then the tmon.out will get complete output)
- Go to the directory where tmon is (logs/dprof/
/) and run dprofpp
Refer: Code Profiling Techniques
Monday, June 04, 2007
Apache and strace
To display a trace of system calls. Useful for debugging.
root@sri# apachectl stop (First stop Apache and then restart Apache with strace)
root@sri# strace -f -o trace.txt /etc/rc.d/init.d/httpd start
-f: Traces child processes as they are created by currently traced processes.
-o: Outputs to a text file
Saturday, November 04, 2006
Perl Tips
AUTOLOAD
Just try the following script:
sub AUTOLOAD {
my $program = our $AUTOLOAD;
$program =~ s/.*:://;
system($program, @_);
}
date();
ls('-l');
whoami();
tree();
# Similarly you can try any command-line commands as above
When Perl encounters an undefined function and that function is not defined, it looks for the function called AUTOLOAD. If one exists, it's called with the same arguments as the original function would have had.
Compare big strings with Logical exclusive-OR
There are 2 strings $string1 and $string2, and both are almost similar... but with a few changes. Supposing if you would like to know the character position where the both strings differ, try the following:
my $string_xor = ("$string1" ^ "$string2");
$string_xor =~ /^(\0*)/;
print "Position = " . length($1) ."\n";
The XOR operator (^) will return a string where every matching byte of which will be null, every mismatching byte will have some bit set. The second statement will catch all the non-null characters from the begining. Its length is the position where both the strings differ.
The ^ operator performs a logical exclusive-OR. The truth table looks like
a b output
0 0 0
0 1 1
1 0 1
1 1 0
Ofcourse, there are also other ways of comparing two strings. For instance you can do something like this:
$strcnt=0;
while (substr($string1,$strcnt,1) eq substr($string2,$strcnt,1)) {$strcnt++}
It is up to u to choose ur flavor.
Simple but cheeeeeky Array manipulations
@array=(1, 2, 3, 4, 5, 6, 7, 8);
#To just retain the first 5 elements, and delete the rest
$#array=5;
print join("\n",@array);
#To chop the last two elements
$#array -= 2;
print join("\n",@array);
Perl XML
Dont know which XML module will suit your need?
Here is a very good article: http://perl-xml.sourceforge.net/faq/
A module to explain ur regular expressions!
Confused with regular expressions? Reading some other code and dont know what a particular regular expression do? Here is a kalaasal module that might help u: YAPE::Regex::Explain
Say you have the regular expression "([^>]+?)\..*", and want to know what it will match. Then try this:
use YAPE::Regex::Explain;
my $myregex='([^>]+?)\..*';
print YAPE::Regex::Explain->new($myregex)->explain;
The output of the above code will be:
The regular expression:
(?-imsx:([^>]+?)\..*)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[^>]+? any character except: '>' (1 or more
times (matching the least amount
possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
\. '.'
----------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
Coooool.. right?
Perl - BEGIN... END
You wud have seen BEGIN, END etc in some perl codes. Here is it what it does (from chapter 18 of Prgramming Perl http://www.oreilly.com/catalog/pperl3/chapter/ch18.html ) :
Just try the following script:
sub AUTOLOAD {
my $program = our $AUTOLOAD;
$program =~ s/.*:://;
system($program, @_);
}
date();
ls('-l');
whoami();
tree();
# Similarly you can try any command-line commands as above
When Perl encounters an undefined function and that function is not defined, it looks for the function called AUTOLOAD. If one exists, it's called with the same arguments as the original function would have had.
Compare big strings with Logical exclusive-OR
There are 2 strings $string1 and $string2, and both are almost similar... but with a few changes. Supposing if you would like to know the character position where the both strings differ, try the following:
my $string_xor = ("$string1" ^ "$string2");
$string_xor =~ /^(\0*)/;
print "Position = " . length($1) ."\n";
The XOR operator (^) will return a string where every matching byte of which will be null, every mismatching byte will have some bit set. The second statement will catch all the non-null characters from the begining. Its length is the position where both the strings differ.
The ^ operator performs a logical exclusive-OR. The truth table looks like
a b output
0 0 0
0 1 1
1 0 1
1 1 0
Ofcourse, there are also other ways of comparing two strings. For instance you can do something like this:
$strcnt=0;
while (substr($string1,$strcnt,1) eq substr($string2,$strcnt,1)) {$strcnt++}
It is up to u to choose ur flavor.
Simple but cheeeeeky Array manipulations
@array=(1, 2, 3, 4, 5, 6, 7, 8);
#To just retain the first 5 elements, and delete the rest
$#array=5;
print join("\n",@array);
#To chop the last two elements
$#array -= 2;
print join("\n",@array);
Perl XML
Dont know which XML module will suit your need?
Here is a very good article: http://perl-xml.sourceforge
A module to explain ur regular expressions!
Confused with regular expressions? Reading some other code and dont know what a particular regular expression do? Here is a kalaasal module that might help u: YAPE::Regex::Explain
Say you have the regular expression "([^>]+?)\..*", and want to know what it will match. Then try this:
use YAPE::Regex::Explain;
my $myregex='([^>]+?)\..*';
print YAPE::Regex::Explain->new(
The output of the above code will be:
The regular expression:
(?-imsx:([^>]+?)\..*)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[^>]+? any character except: '>' (1 or more
times (matching the least amount
possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
\. '.'
----------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
Coooool.. right?
Perl - BEGIN... END
You wud have seen BEGIN, END etc in some perl codes. Here is it what it does (from chapter 18 of Prgramming Perl http://www.oreilly.com/catalog
These four block types run in this order:
- BEGIN: Runs ASAP (as soon as parsed) whenever encountered during compilation, before compiling the rest of the file.
- CHECK: Runs when compilation is complete, but before the program starts. (CHECK can mean "checkpoint" or "double-check" or even just "stop".)
- INIT: Runs at the beginning of execution right before the main flow of your program starts.
- END: Runs at the end of execution right after the program finishes.
- If you declare more than one of these by the same name, even in separate modules, the BEGINs all run before any CHECKs, which all run before any INITs, which all run before any ENDs--which all run dead last, after your main program has finished. Multiple BEGINs and INITs run in declaration order (FIFO), and the CHECKs and ENDs run in inverse declaration order (LIFO).
General Web Tips
Favicon
You might have noticed that when you visit some web sites, there will be a small image that appears at the top of your browser... as well as in the URL addressbar (if u use firefox). Wanna know how to do it? It is very simple:
1. Open paintbrush
2. Create an image of size 24x24 pixels
3. Save it as "favicon.ico"
4. Upload the favicon.ico image to your website root directory. The root directory is the main folder where all your files reside at, like your main index page.
Thats it.
IE behaves differently. It will not showup the icon in the address bar like Mozilla Firefox when you just visit a page. But it will only work, if you have bookmarked that web page. So in IE:
1. bookmark your page
2. then visit that page
Also make sure u have the following line in the :
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="icon" href="favicon.ico" type="image/x-icon">
Mozilla Firefox works without the above two lines also.
I tested it and it works fine in Mozilla as well as in IE 6. For more info refer: http://en.wikipedia.org/wiki/Favicon and http://www.chami.com/HTML-kit/services/favicon/help/
Apache log - more detailed entries
You want to add a little more detail to your access log entries?
Use the --combined-- log format rather than --common-- log format:
CustomLog logs/access_log combined
The --combined-- log format offers two additional pieces of information not included in the --common-- log format:
1. the Referer (where the clinet linked from) and
2. the User-agent (what browser they are using)
Source: Apache Cookbook
Web protocols - Fundamentals
Good article on Web protocols by Ryan Tomayko - "How I Explained REST to My Wife":
http://naeblis.cx/articles/2004/12/12/rest-to-my-wife
You might have noticed that when you visit some web sites, there will be a small image that appears at the top of your browser... as well as in the URL addressbar (if u use firefox). Wanna know how to do it? It is very simple:
1. Open paintbrush
2. Create an image of size 24x24 pixels
3. Save it as "favicon.ico"
4. Upload the favicon.ico image to your website root directory. The root directory is the main folder where all your files reside at, like your main index page.
Thats it.
IE behaves differently. It will not showup the icon in the address bar like Mozilla Firefox when you just visit a page. But it will only work, if you have bookmarked that web page. So in IE:
1. bookmark your page
2. then visit that page
Also make sure u have the following line in the :
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="icon" href="favicon.ico" type="image/x-icon">
Mozilla Firefox works without the above two lines also.
I tested it and it works fine in Mozilla as well as in IE 6. For more info refer: http://en.wikipedia.org/wiki
Apache log - more detailed entries
You want to add a little more detail to your access log entries?
Use the --combined-- log format rather than --common-- log format:
CustomLog logs/access_log combined
The --combined-- log format offers two additional pieces of information not included in the --common-- log format:
1. the Referer (where the clinet linked from) and
2. the User-agent (what browser they are using)
Source: Apache Cookbook
Web protocols - Fundamentals
Good article on Web protocols by Ryan Tomayko - "How I Explained REST to My Wife":
http://naeblis.cx/articles
Friday, November 03, 2006
OpenSSL
In simple terms: Can be used to generate the keys that a web server needs to encrypt the data sent between the client and the server
Process:
1. A client browser connects to the Apache HTTP server via a Web request
2. Browser asks to start a secure session with the server.
3. Server returns the site's certificate which also includes the server public key
4. The browser analyzes the certificate
5. Informs the user about its validity
6. Browser creates a session key, encrypted with server's public key
7. It is sent to the server
8. Server decrypts using its private key
Now, Both the browser and the server now are using the same session key. This is a symmetric key used to encrypt and decrypt data exchanged by the browser and server
Process:
1. A client browser connects to the Apache HTTP server via a Web request
2. Browser asks to start a secure session with the server.
3. Server returns the site's certificate which also includes the server public key
4. The browser analyzes the certificate
5. Informs the user about its validity
6. Browser creates a session key, encrypted with server's public key
7. It is sent to the server
8. Server decrypts using its private key
Now, Both the browser and the server now are using the same session key. This is a symmetric key used to encrypt and decrypt data exchanged by the browser and server
Subscribe to:
Posts (Atom)