Palm Plugin: fix thread key var name
[MonkeyD.git] / plugins / palm / request.c
blobbe0bd6571d83002ed9c0a0e839ec9dfa2c1f0ff3
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /* Monkey HTTP Daemon
4 * ------------------
5 * Copyright (C) 2001-2010, Eduardo Silva P.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Library General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <pthread.h>
23 #include <stdio.h>
25 #include "config.h"
26 #include "plugin.h"
27 #include "monkey.h"
28 #include "palm.h"
30 struct mk_palm_request *mk_palm_request_create(int client_fd,
31 int palm_fd,
32 struct client_request *cr,
33 struct request *sr,
34 struct mk_palm *palm)
36 struct mk_palm_request *new;
38 new = mk_api->mem_alloc(sizeof(struct mk_palm_request));
39 new->client_fd = client_fd;
40 new->palm_fd = palm_fd;
41 new->palm = palm;
42 new->bytes_sent = 0;
43 new->bytes_read = 0;
44 new->headers_sent = VAR_OFF;
45 new->cr = cr;
46 new->sr = sr;
47 new->next = NULL;
49 return new;
52 void mk_palm_request_add(struct mk_palm_request *pr)
54 struct mk_palm_request *pr_list, *aux;
56 /* Get thread data */
57 pr_list = pthread_getspecific(_mkp_data);
59 /* No connection previously was found */
60 if(!pr_list) {
61 pthread_setspecific(_mkp_data, pr);
62 return;
65 /* Add Node */
66 aux = pr_list;
67 while(aux->next){
68 aux = aux->next;
71 aux->next = pr;
72 pthread_setspecific(_mkp_data, pr_list);
75 /* It register the request and connection data, if it doesn't
76 * exists it will be create it, otherwise will return the pointer
77 * to the mk_palm_request struct node
79 struct mk_palm_request *mk_palm_request_get(int socket)
81 struct mk_palm_request *pr, *aux;
83 /* Get thread data */
84 pr = pthread_getspecific(_mkp_data);
86 /* No connection previously was found */
87 if(!pr) {
88 return NULL;
91 /* Look for node */
92 aux = pr;
93 while(aux){
94 if(aux->palm_fd == socket){
95 return aux;
97 aux = aux->next;
100 return NULL;
103 struct mk_palm_request *mk_palm_request_get_by_http(int socket)
105 struct mk_palm_request *pr, *aux;
107 /* Get thread data */
108 pr = pthread_getspecific(_mkp_data);
110 /* No connection previously was found */
111 if(!pr) {
112 return NULL;
115 /* Look for node */
116 aux = pr;
117 while(aux){
118 if(aux->client_fd == socket){
119 return aux;
121 aux = aux->next;
124 return NULL;
127 void mk_palm_request_update(int socket, struct mk_palm_request *pr)
129 struct mk_palm_request *aux, *list;
131 list = pthread_getspecific(_mkp_data);
133 if (!list) {
134 return;
137 aux = list;
138 while (aux) {
139 if (aux->palm_fd == socket) {
140 aux->bytes_sent = pr->bytes_sent;
141 aux->bytes_read = pr->bytes_read;
142 aux->headers_sent = pr->headers_sent;
144 /* Update data */
145 pthread_setspecific(_mkp_data, list);
146 return;
148 aux = aux->next;
152 void mk_palm_request_delete(int socket)
154 struct mk_palm_request *aux, *prev, *list;
156 list = pthread_getspecific(_mkp_data);
158 if (!list) {
159 return;
162 aux = list;
163 while(aux) {
164 if (aux->palm_fd == socket) {
165 /* first node */
166 if (aux == list) {
167 list = aux->next;
169 else {
170 prev = list;
171 while(prev->next != aux) {
172 prev = prev->next;
174 prev->next = aux->next;
176 mk_api->mem_free(aux);
177 pthread_setspecific(_mkp_data, list);
178 return;
180 aux = aux->next;
184 void mk_palm_free_request(int sockfd)
186 /* get palm request node */
187 mk_palm_request_get(sockfd);
188 /* delete palm request node */
189 mk_palm_request_delete(sockfd);
190 mk_api->socket_close(sockfd);