Project

General

Profile

Bug #1747 ยป network_openssl.c.patch

patch for v1.5-svn - HenrikHolst, 2008-08-08 13:20

View differences:

network_openssl.c (working copy)
250 250
			ssize_t r;
251 251
			stat_cache_entry *sce = NULL;
252 252
			int ifd;
253
			int write_wait = 0;
254 253

  
255 254
			if (HANDLER_ERROR == stat_cache_get_entry(srv, con, c->file.name, &sce)) {
256 255
				ERROR("stat_cache_get_entry(%s) failed: %s", SAFE_BUF_STR(c->file.name), strerror(errno));
......
263 262
				assert(local_send_buffer);
264 263
			}
265 264

  
266
			do {
267
				off_t offset = c->file.start + c->offset;
268
				off_t toSend = c->file.length - c->offset;
265
			off_t offset = c->file.start + c->offset;
266
			off_t toSend = c->file.length - c->offset;
269 267

  
270
				if (toSend > LOCAL_SEND_BUFSIZE) toSend = LOCAL_SEND_BUFSIZE;
268
			if (toSend > LOCAL_SEND_BUFSIZE) toSend = LOCAL_SEND_BUFSIZE;
271 269

  
272
				if (-1 == (ifd = open(BUF_STR(c->file.name), O_RDONLY))) {
273
					ERROR("open(%s) failed: %s", SAFE_BUF_STR(c->file.name), strerror(errno));
270
			if (-1 == (ifd = open(BUF_STR(c->file.name), O_RDONLY))) {
271
				ERROR("open(%s) failed: %s", SAFE_BUF_STR(c->file.name), strerror(errno));
274 272

  
275
					return NETWORK_STATUS_FATAL_ERROR;
276
				}
273
				return NETWORK_STATUS_FATAL_ERROR;
274
			}
277 275

  
278 276

  
279
				lseek(ifd, offset, SEEK_SET);
280
				if (-1 == (toSend = read(ifd, local_send_buffer, toSend))) {
281
					close(ifd);
277
			lseek(ifd, offset, SEEK_SET);
278
			if (-1 == (toSend = read(ifd, local_send_buffer, toSend))) {
279
				close(ifd);
282 280
					
283
					ERROR("read(%s) failed: %s", SAFE_BUF_STR(c->file.name), strerror(errno));
281
				ERROR("read(%s) failed: %s", SAFE_BUF_STR(c->file.name), strerror(errno));
284 282

  
285
					return NETWORK_STATUS_FATAL_ERROR;
286
				}
283
				return NETWORK_STATUS_FATAL_ERROR;
284
			}
287 285

  
288
				s = local_send_buffer;
286
			s = local_send_buffer;
289 287

  
290
				close(ifd);
288
			close(ifd);
291 289

  
292
				ERR_clear_error();
293
				if ((r = SSL_write(sock->ssl, s, toSend)) <= 0) {
294
					unsigned long err;
290
			ERR_clear_error();
291
			if ((r = SSL_write(sock->ssl, s, toSend)) <= 0) {
292
				unsigned long err;
295 293

  
296
					switch ((ssl_r = SSL_get_error(sock->ssl, r))) {
297
					case SSL_ERROR_WANT_WRITE:
298
						write_wait = 1;
299
						break;
300
					case SSL_ERROR_SYSCALL:
301
						/* perhaps we have error waiting in our error-queue */
302
						if (0 != (err = ERR_get_error())) {
303
							do {
304
								ERROR("SSL_write(): ssl-error: %d (ret = %zd): %s",
305
										ssl_r, r,
306
										ERR_error_string(err, NULL));
307
							} while((err = ERR_get_error()));
308
						} else if (r == -1) {
309
							/* no, but we have errno */
310
							switch(errno) {
311
							case EPIPE:
312
							case ECONNRESET:
313
								return NETWORK_STATUS_CONNECTION_CLOSE;
314
							default:
315
								ERROR("SSL_write(): ssl-error: %d (ret = %zd). errno=%d, %s",
316
										ssl_r, r, errno,
317
										strerror(errno));
318
								break;
319
							}
320
						} else {
294
				switch ((ssl_r = SSL_get_error(sock->ssl, r))) {
295
				case SSL_ERROR_WANT_WRITE:
296
					break;
297
				case SSL_ERROR_SYSCALL:
298
					/* perhaps we have error waiting in our error-queue */
299
					if (0 != (err = ERR_get_error())) {
300
						do {
301
							ERROR("SSL_write(): ssl-error: %d (ret = %zd): %s",
302
									ssl_r, r,
303
									ERR_error_string(err, NULL));
304
						} while((err = ERR_get_error()));
305
					} else if (r == -1) {
306
						/* no, but we have errno */
307
						switch(errno) {
308
						case EPIPE:
309
						case ECONNRESET:
310
							return NETWORK_STATUS_CONNECTION_CLOSE;
311
						default:
321 312
							ERROR("SSL_write(): ssl-error: %d (ret = %zd). errno=%d, %s",
322
										ssl_r, r, errno,
323
										strerror(errno));
313
									ssl_r, r, errno,
314
									strerror(errno));
315
							break;
324 316
						}
317
					} else {
318
						ERROR("SSL_write(): ssl-error: %d (ret = %zd). errno=%d, %s",
319
									ssl_r, r, errno,
320
									strerror(errno));
321
					}
325 322

  
326
						return  NETWORK_STATUS_FATAL_ERROR;
327
					case SSL_ERROR_ZERO_RETURN:
328
						/* clean shutdown on the remote side */
323
					return  NETWORK_STATUS_FATAL_ERROR;
324
				case SSL_ERROR_ZERO_RETURN:
325
					/* clean shutdown on the remote side */
329 326

  
330
						if (r == 0)  return NETWORK_STATUS_CONNECTION_CLOSE;
327
					if (r == 0)  return NETWORK_STATUS_CONNECTION_CLOSE;
331 328

  
332
						/* fall thourgh */
333
					default:
334
						while((err = ERR_get_error())) {
335
							ERROR("SSL_write(): ssl-error: %d (ret = %zd), %s",
336
									ssl_r, r,
337
									ERR_error_string(err, NULL));
338
						}
339

  
340
						return NETWORK_STATUS_FATAL_ERROR;
329
					/* fall thourgh */
330
				default:
331
					while((err = ERR_get_error())) {
332
						ERROR("SSL_write(): ssl-error: %d (ret = %zd), %s",
333
								ssl_r, r,
334
								ERR_error_string(err, NULL));
341 335
					}
342
				} else {
343
					c->offset += r;
344
					cq->bytes_out += r;
345
				}
346 336

  
347
				if (c->offset == c->file.length) {
348
					chunk_finished = 1;
337
					return NETWORK_STATUS_FATAL_ERROR;
349 338
				}
350
			} while(!chunk_finished && !write_wait);
339
			} else {
340
				c->offset += r;
341
				cq->bytes_out += r;
342
			}
351 343

  
344
			if (c->offset == c->file.length) {
345
				chunk_finished = 1;
346
			}
347

  
352 348
			break;
353 349
		}
354 350
		default:
    (1-1/1)