void send_error_page(client_t *client) { shutdown(client->fd, SHUT_RD); if(client->header_done){ //already sended response data //close connection return; } int status = client->bad_request_code; int r = status < 0 ? status * -1 : status; client->status_code = r; switch(r){ case 400: blocking_write(client, MSG_400, sizeof(MSG_400) -1); break; case 411: blocking_write(client, MSG_411, sizeof(MSG_411) -1); break; case 413: blocking_write(client, MSG_413, sizeof(MSG_413) -1); break; default: //Internal Server Error blocking_write(client, MSG_500, sizeof(MSG_500) -1); break; } client->keep_alive = 0; client->header_done = 1; client->response_closed = 1; }
/* buffering_write() - buffers data to a specified size before writing. * * Restrictions: * - MUST CALL BUFFERING_CLOSE() WHEN FINISHED!!! * */ long buffering_write(int fd, char *buffer, long num) { if (fd != bw_fd) { /* clean up after buffering for some other file */ if (bw_fd >= 0 && bw_pos > 0) { if (blocking_write(bw_fd, bw_outbuf, bw_pos)) { perror("write (in buffering_write, flushing)"); } } bw_fd = fd; bw_pos = 0; } if (bw_pos + num > OUTBUFSZ) { /* fill our buffer first, then write, then modify buffer and num */ memcpy(&bw_outbuf[bw_pos], buffer, OUTBUFSZ - bw_pos); if (blocking_write(fd, bw_outbuf, OUTBUFSZ)) { perror("write (in buffering_write, full buffer)"); return(-1); } num -= (OUTBUFSZ - bw_pos); buffer += (OUTBUFSZ - bw_pos); bw_pos = 0; } /* save data */ memcpy(&bw_outbuf[bw_pos], buffer, num); bw_pos += num; return(0); }
/* buffering_close() - writes out remaining buffered data before closing * file. * */ int buffering_close(int fd) { if (fd == bw_fd && bw_pos > 0) { /* write out remaining data and clean up */ if (blocking_write(fd, bw_outbuf, bw_pos)) { perror("write (in buffering_close)"); } bw_fd = -1; bw_pos = 0; } return(close(fd)); }
void send_error_page(client_t *client) { shutdown(client->fd, SHUT_RD); if(client->header_done || client->response_closed){ // already sended response data // close connection return; } /* int status = client->bad_request_code; */ /* int r = status < 0 ? status * -1:status; */ /* client->status_code = client->bad_request_code; */ DEBUG("send_error_page status_code %d client %p", client->status_code, client); switch(client->status_code){ case 400: blocking_write(client, MSG_400, sizeof(MSG_400) -1); client->write_bytes -= sizeof(H_MSG_400) -1; break; case 408: blocking_write(client, MSG_408, sizeof(MSG_408) -1); client->write_bytes -= sizeof(H_MSG_408) -1; break; case 411: blocking_write(client, MSG_411, sizeof(MSG_411) -1); client->write_bytes -= sizeof(H_MSG_411) -1; break; case 413: blocking_write(client, MSG_413, sizeof(MSG_413) -1); client->write_bytes -= sizeof(H_MSG_413) -1; break; case 417: blocking_write(client, MSG_417, sizeof(MSG_417) -1); client->write_bytes -= sizeof(H_MSG_417) -1; break; case 503: blocking_write(client, MSG_503, sizeof(MSG_503) -1); client->write_bytes -= sizeof(H_MSG_503) -1; break; default: //Internal Server Error blocking_write(client, MSG_500, sizeof(MSG_500) -1); client->write_bytes -= sizeof(H_MSG_500) -1; break; } client->keep_alive = 0; client->header_done = 1; client->response_closed = 1; }
static bool sparse_extract_region (struct tar_sparse_file *file, size_t i) { off_t write_size; if (!lseek_or_error (file, file->stat_info->sparse_map[i].offset)) return false; write_size = file->stat_info->sparse_map[i].numbytes; if (write_size == 0) { /* Last block of the file is a hole */ if (file->seekable && sys_truncate (file->fd)) truncate_warn (file->stat_info->orig_file_name); } else while (write_size > 0) { size_t count; size_t wrbytes = (write_size > BLOCKSIZE) ? BLOCKSIZE : write_size; union block *blk = find_next_block (); if (!blk) { ERROR ((0, 0, _("Unexpected EOF in archive"))); return false; } set_next_block_after (blk); count = blocking_write (file->fd, blk->buffer, wrbytes); write_size -= count; file->dumped_size += count; mv_size_left (file->stat_info->archive_file_size - file->dumped_size); file->offset += count; if (count != wrbytes) { write_error_details (file->stat_info->orig_file_name, count, wrbytes); return false; } } return true; }