crossroads

Git mirror of https://crossroads.e-tunity.com/
git clone git://git.finwo.net/app/crossroads
Log | Files | Refs

httpserversocket.c (2524B)


      1 /*************************************************************************
      2  * This file is part of Crosroads 1.23, a load balancer and fail over
      3  * utility for TCP. Copyright (c) Karel Kubat, distributed under GPL.
      4  * Visit http://crossroads.e-tunity.com for information.
      5  *************************************************************************/
      6 #include "crossroads.h"
      7 
      8 int http_serversocket (HttpHeader *m, int *is_continuation) {
      9     int i, sock;
     10 
     11     msg ("Service %s: searching for back end for HTTP request.",
     12 	 activeservice->name);
     13 
     14     /* Try to find a sticky cookie in the request. */
     15     for (i = 0; i < activeservice->nbackend; i++) {
     16 	if (servicereport->backendstate[i].avail != st_available)
     17 	    continue;
     18 	if (activeservice->backend[i].maxconnections > 0 &&
     19 	    activeservice->backend[i].maxconnections <=
     20 	    servicereport->backendstate[i].nclients)
     21 	    continue;
     22 	if (http_header_hascookie (m,
     23 				   activeservice->backend[i].stickycookie)) {
     24 	    msg ("Service %s: HTTP backend %d selected due to cookie '%s'",
     25 		 activeservice->name,
     26 		 i, 
     27 		 activeservice->backend[i].stickycookie);
     28 
     29 	    /* Got the target back end. Try to connect to it.
     30 	     * If it doesn't succeed, then we'll do a failover to
     31 	     * any other back end. */
     32 	    current_backend = i;
     33 	    if ( (sock = backend_connect ()) >= 0 ) {
     34 		*is_continuation = 1;
     35 		set_program_title ("Service %s: serving %s to %s",
     36 				   activeservice->name,
     37 				   client_ip,
     38 				   activeservice->backend[current_backend]
     39 				       .name);
     40 		return (sock);
     41 	    }
     42 	}
     43     }
     44 
     45     /* We got here because this is the first time 'round or because
     46      * a previously established back end went dead in the middle of a
     47      * session. So: Loop 'till we find a back end, or until we fail. */
     48     while (1) {
     49 	/* No back end? Nogo. */
     50 	if (! backend_count()) {
     51 	    msg ("Service %s: "
     52 		 "out of back ends while scanning for HTTP back end",
     53 		 activeservice->name);
     54 	    return (-1);
     55 	}
     56 
     57 	/* Try one. */
     58 	choose_backend();
     59 	if (current_backend < 0) {
     60 	    warning ("No back end for request.");
     61 	    return (-2);
     62 	}
     63 
     64 	msg ("Service %s: trying back end %d for new HTTP connection",
     65 	     activeservice->name, current_backend);
     66 	if ( (sock = backend_connect ()) >= 0 ) {
     67 	    servicereport->backendstate[current_backend].sessions++;
     68 	    *is_continuation = 0;
     69 	    set_program_title ("Service %s: serving %s to %s",
     70 			       activeservice->name,
     71 			       client_ip,
     72 			       activeservice->backend[current_backend].name);
     73 	    return (sock);
     74 	}
     75     }
     76 }