static HRESULT WINAPI mxwriter_saxcontent_startDocument(ISAXContentHandler *iface) { mxwriter *This = impl_from_ISAXContentHandler( iface ); TRACE("(%p)\n", This); /* If properties have been changed since the last "endDocument" call * we need to reset the output buffer. If we don't the output buffer * could end up with multiple XML documents in it, plus this seems to * be how Windows works. */ if (This->prop_changed) { reset_output_buffer(This); This->prop_changed = FALSE; } if (This->props[MXWriter_OmitXmlDecl] == VARIANT_TRUE) return S_OK; write_prolog_buffer(This); if (This->dest && This->xml_enc == XmlEncoding_UTF16) { static const char utf16BOM[] = {0xff,0xfe}; if (This->props[MXWriter_BOM] == VARIANT_TRUE) /* Windows passes a NULL pointer as the pcbWritten parameter and * ignores any error codes returned from this Write call. */ IStream_Write(This->dest, utf16BOM, sizeof(utf16BOM), NULL); } return S_OK; }
static HRESULT WINAPI mxwriter_put_encoding(IMXWriter *iface, BSTR encoding) { mxwriter *This = impl_from_IMXWriter( iface ); xml_encoding enc; HRESULT hr; TRACE("(%p)->(%s)\n", This, debugstr_w(encoding)); enc = parse_encoding_name(encoding); if (enc == XmlEncoding_Unknown) { FIXME("unsupported encoding %s\n", debugstr_w(encoding)); return E_INVALIDARG; } hr = flush_output_buffer(This); if (FAILED(hr)) return hr; SysReAllocString(&This->encoding, encoding); This->xml_enc = enc; TRACE("got encoding %d\n", This->xml_enc); reset_output_buffer(This); return S_OK; }
static HRESULT WINAPI mxwriter_put_output(IMXWriter *iface, VARIANT dest) { mxwriter *This = impl_from_IMXWriter( iface ); HRESULT hr; TRACE("(%p)->(%s)\n", This, debugstr_variant(&dest)); hr = flush_output_buffer(This); if (FAILED(hr)) return hr; switch (V_VT(&dest)) { case VT_EMPTY: { if (This->dest) IStream_Release(This->dest); This->dest = NULL; reset_output_buffer(This); break; } case VT_UNKNOWN: { IStream *stream; hr = IUnknown_QueryInterface(V_UNKNOWN(&dest), &IID_IStream, (void**)&stream); if (hr == S_OK) { /* Recreate the output buffer to make sure it's using the correct encoding. */ reset_output_buffer(This); if (This->dest) IStream_Release(This->dest); This->dest = stream; break; } FIXME("unhandled interface type for VT_UNKNOWN destination\n"); return E_NOTIMPL; } default: FIXME("unhandled destination type %s\n", debugstr_variant(&dest)); return E_NOTIMPL; } return S_OK; }
int audio_get(request * req, int video_type) { AV_DATA av_data; int ret; if (req->audio_book == 0) { GetAVData(AV_OP_GET_ULAW_SERIAL, -1, &av_data ); if (av_data.serial <= req->audio_lock) { dbg("av_data.serial <= req->audio_lock!!!\n"); req->busy_flag &= ~BUSY_FLAG_AUDIO; return 0; } req->audio_book = av_data.serial; } ret = GetAVData(AV_OP_LOCK_ULAW, req->audio_book, &av_data ); if (ret == RET_SUCCESS) { extern int CheckAudioStatus(void); if (req->audio_lock > 0) { GetAVData(AV_OP_UNLOCK_ULAW, req->audio_lock, NULL); req->audio_lock = 0; } if(CheckAudioStatus()) { req->data_mem = av_data.ptr; }else{ static int IsFirst = 1; if(IsFirst) { IsFirst = 0; memset(AUDIO_MUTE_ARRAY,0xff,sizeof(AUDIO_MUTE_ARRAY)); } req->data_mem = AUDIO_MUTE_ARRAY; } req->filesize = av_data.size; req->filepos = 0; req->audio_lock = av_data.serial; req->audio_book = av_data.serial + 1; req->audio_sync = av_data.ref_serial[video_type]; reset_output_buffer(req); req_write(req, "\r\n"); print_audio_headers(req); return 1; } else if (ret == RET_NO_VALID_DATA) { req->busy_flag &= ~BUSY_FLAG_AUDIO; return 0; } else { req->audio_book = 0; dbg("ERROR, ret=%d\n", ret); return -1; } }
void audio_send(request * req) { req->data_mem = req->audio_data; req->filesize = req->audio_length; req->filepos = 0; req->audio_length = 0; reset_output_buffer(req); req_write(req, "\r\n"); print_audio_headers(req); }
uint8_t transmit_data() { if(!have_data_to_transmit()) { return SUCCESS; } if(hold_transmit_flag) return SUCCESS; reset_ack(); uint16_t end_ptr = write_index; while (have_data_to_transmit()) { uart_send_array(&output_buffer[read_index], end_ptr-read_index); if(wait_for(&have_ack, 500)) { read_index = end_ptr; } else { return FAILURE; } end_ptr = write_index; } reset_output_buffer(); return SUCCESS; }
int init_get(request * req) { int data_fd, saved_errno; struct stat statbuf; volatile off_t bytes_free; data_fd = open(req->pathname, O_RDONLY|O_LARGEFILE); saved_errno = errno; /* might not get used */ while (use_lang_rewrite && data_fd == -1 && errno == ENOENT) { /* We cannot open that file - Check whether we can rewrite it * to a different language suffix. We only support filenames * of the format: "foo.ll.html" as an alias for "foo.html". */ unsigned int len; len = strlen(req->pathname); if (len < 6 || strcmp (req->pathname + len - 5, ".html")) break; /* does not end in ".html" */ if (len > 8 && req->pathname[len-8] == '.' && req->pathname[len-7] >= 'a' && req->pathname[len-7] <= 'z' && req->pathname[len-6] >= 'a' && req->pathname[len-6] <= 'z') { /* The request was for a language dependent file. Strip * it and try the generic form. */ char save_name[8]; strcpy (save_name, req->pathname + len - 7); strcpy (req->pathname + len - 7, "html"); data_fd = open(req->pathname, O_RDONLY); if (data_fd == -1) strcpy (req->pathname + len - 7, save_name); break; } else if ( 0 ) { /* Fixme: Other items to try from the list of accepted_languages */ data_fd = open(req->pathname, O_RDONLY); } else break; } #ifdef GUNZIP if (data_fd == -1 && errno == ENOENT) { /* cannot open */ /* it's either a gunzipped file or a directory */ char gzip_pathname[MAX_PATH_LENGTH]; unsigned int len; len = strlen(req->pathname); if (len + 4 > sizeof(gzip_pathname)) { log_error_doc(req); fprintf(stderr, "Pathname + .gz too long! (%s)\n", req->pathname); send_r_bad_request(req); return 0; } memcpy(gzip_pathname, req->pathname, len); memcpy(gzip_pathname + len, ".gz", 3); gzip_pathname[len + 3] = '\0'; data_fd = open(gzip_pathname, O_RDONLY|O_LARGEFILE); if (data_fd != -1) { close(data_fd); req->response_status = R_REQUEST_OK; if (req->pathname) free(req->pathname); req->pathname = strdup(gzip_pathname); if (!req->pathname) { boa_perror(req, "strdup req->pathname for gzipped filename " __FILE__ ":" STR(__LINE__)); return 0; } if (req->http_version != HTTP09) { req_write(req, http_ver_string(req->http_version)); req_write(req, " 200 OK-GUNZIP" CRLF); print_http_headers(req); print_content_type(req); print_last_modified(req); req_write(req, CRLF); req_flush(req); } if (req->method == M_HEAD) return 0; return init_cgi(req); } } #endif if (data_fd == -1) { log_error_doc(req); errno = saved_errno; perror("document open"); if (saved_errno == ENOENT) send_r_not_found(req); else if (saved_errno == EACCES) send_r_forbidden(req); else send_r_bad_request(req); return 0; } #ifdef ACCESS_CONTROL if (!access_allow(req->pathname)) { send_r_forbidden(req); return 0; } #endif fstat(data_fd, &statbuf); if (S_ISDIR(statbuf.st_mode)) { /* directory */ close(data_fd); /* close dir */ if (req->pathname[strlen(req->pathname) - 1] != '/') { char buffer[3 * MAX_PATH_LENGTH + 128]; unsigned int len; #ifdef ALLOW_LOCAL_REDIRECT len = strlen(req->request_uri); if (len + 2 > sizeof(buffer)) { send_r_error(req); return 0; } memcpy(buffer, req->request_uri, len); buffer[len] = '/'; buffer[len+1] = '\0'; #else char *host = server_name; unsigned int l2; char *port = NULL; const char *prefix = hsts_header? "https://" : "http://"; static unsigned int l3 = 0; static unsigned int l4 = 0; if (l4 == 0) { l4 = strlen(prefix); } len = strlen(req->request_uri); if (!port && server_port != 80 && !no_redirect_port) { port = strdup(simple_itoa(server_port)); if (port == NULL) { errno = ENOMEM; boa_perror(req, "Unable to perform simple_itoa conversion on server port!"); return 0; } l3 = strlen(port); } /* l3 and l4 are done */ if (req->host) { /* only shows up in vhost mode */ /* what about the port? (in vhost_mode?) */ /* we don't currently report ports that differ * from out "bound" (listening) port, so we don't care */ host = req->host; } l2 = strlen(host); if (server_port != 80 && !no_redirect_port) { if (l4 + l2 + 1 + l3 + len + 1 > sizeof(buffer)) { errno = ENOMEM; boa_perror(req, "buffer not large enough for directory redirect"); return 0; } memcpy(buffer, prefix, l4); memcpy(buffer + l4, host, l2); buffer[l4 + l2] = ':'; memcpy(buffer + l4 + l2 + 1, port, l3); memcpy(buffer + l4 + l2 + 1 + l3, req->request_uri, len); buffer[l4 + l2 + 1 + l3 + len] = '/'; buffer[l4 + l2 + 1 + l3 + len + 1] = '\0'; } else { if (l4 + l2 + len + 1 > sizeof(buffer)) { errno = ENOMEM; boa_perror(req, "buffer not large enough for directory redirect"); return 0; } memcpy(buffer, prefix, l4); memcpy(buffer + l4, host, l2); memcpy(buffer + l4 + l2, req->request_uri, len); buffer[l4 + l2 + len] = '/'; buffer[l4 + l2 + len + 1] = '\0'; } #endif /* ALLOW LOCAL REDIRECT */ send_r_moved_perm(req, buffer); return 0; } data_fd = get_dir(req, &statbuf); /* updates statbuf */ if (data_fd < 0) /* couldn't do it */ return 0; /* errors reported by get_dir */ else if (data_fd == 0 || data_fd == 1) return data_fd; /* else, data_fd contains the fd of the file... */ } if (!S_ISREG(statbuf.st_mode)) { /* regular file */ log_error_doc(req); fprintf(stderr, "Resulting file is not a regular file.\n"); send_r_bad_request(req); close(data_fd); return 0; } /* If-UnModified-Since asks * is the file newer than date located in time_cval * yes -> return 412 * no -> return 200 * * If-Modified-Since asks * is the file date less than or same as the date located in time_cval * yes -> return 304 * no -> return 200 * * If-Unmodified-Since overrides If-Modified-Since */ /* if (req->headers[H_IF_UNMODIFIED_SINCE] && modified_since(&(statbuf.st_mtime), req->headers[H_IF_UNMODIFIED_SINCE])) { send_r_precondition_failed(req); return 0; } else */ if (req->if_modified_since && !modified_since(&(statbuf.st_mtime), req->if_modified_since)) { send_r_not_modified(req); close(data_fd); return 0; } req->filesize = statbuf.st_size; req->last_modified = statbuf.st_mtime; /* ignore if-range without range */ if (req->header_ifrange && !req->ranges) req->header_ifrange = NULL; /* we don't support it yet */ req->header_ifrange = NULL; /* parse ranges now */ /* we have to wait until req->filesize exists to fix them up */ /* fixup handles handles communicating with the client */ /* ranges_fixup logs as appropriate, and sends * send_r_invalid_range on error. */ if (req->filesize == 0) { if (req->http_version < HTTP11) { send_r_request_ok(req); close(data_fd); return 0; } send_r_no_content(req); close(data_fd); return 0; } if (req->ranges && !ranges_fixup(req)) { close(data_fd); return 0; } /* if no range has been set, use default range */ #if 0 DEBUG(DEBUG_RANGE) { log_error_time(); fprintf(stderr, "if-range: %s\time_cval: %d\tmtime: %d\n", req->header_ifrange, req->time_cval, statbuf->st_mtime); } #endif /* If the entity tag given in the If-Range header matches the current entity tag for the entity, then the server should provide the specified sub-range of the entity using a 206 (Partial content) response. If the entity tag does not match, then the server should return the entire entity using a 200 (OK) response. */ /* IF we have range data *and* no if-range or if-range matches... */ #ifdef MAX_FILE_MMAP if (req->filesize > MAX_FILE_MMAP) { req->data_fd = data_fd; req->status = IOSHUFFLE; } else #endif { /* NOTE: I (Jon Nelson) tried performing a read(2) * into the output buffer provided the file data would * fit, before mmapping, and if successful, writing that * and stopping there -- all to avoid the cost * of a mmap. Oddly, it was *slower* in benchmarks. */ req->mmap_entry_var = find_mmap(data_fd, &statbuf); if (req->mmap_entry_var == NULL) { req->data_fd = data_fd; req->status = IOSHUFFLE; } else { req->data_mem = req->mmap_entry_var->mmap; close(data_fd); /* close data file */ } } if (!req->ranges) { req->ranges = range_pool_pop(); req->ranges->start = 0; req->ranges->stop = -1; if (!ranges_fixup(req)) { return 0; } send_r_request_ok(req); } else { /* FIXME: support if-range header here, by the following logic: * if !req->header_ifrange || st_mtime > header_ifrange, * send_r_partial_content * else * reset-ranges, etc... */ if (!req->header_ifrange) { send_r_partial_content(req); } else { /* either no if-range or the if-range does not match */ ranges_reset(req); req->ranges = range_pool_pop(); req->ranges->start = 0; req->ranges->stop = -1; if (!ranges_fixup(req)) { return 0; } send_r_request_ok(req); } } if (req->method == M_HEAD) { return complete_response(req); } bytes_free = 0; if (req->data_mem) { /* things can really go tilt if req->buffer_end > BUFFER_SIZE, * but basically that can't happen */ /* We lose statbuf here, so make sure response has been sent */ bytes_free = BUFFER_SIZE - req->buffer_end; /* 256 bytes for the **trailing** headers */ /* bytes is now how much the buffer can hold * after the headers */ } if (req->data_mem && bytes_free > 256) { unsigned int want; Range *r; r = req->ranges; want = (r->stop - r->start) + 1; if (bytes_free > want) bytes_free = want; else { /* bytes_free <= want */ ; } if (setjmp(env) == 0) { handle_sigbus = 1; memcpy(req->buffer + req->buffer_end, req->data_mem + r->start, bytes_free); handle_sigbus = 0; /* OK, SIGBUS **after** this point is very bad! */ } else { /* sigbus! */ log_error_doc(req); reset_output_buffer(req); send_r_error(req); log_error("Got SIGBUS in memcpy\n"); return 0; } req->buffer_end += bytes_free; req->bytes_written += bytes_free; r->start += bytes_free; if (bytes_free == want) { /* this will fit due to the 256 extra bytes_free */ return complete_response(req); } } /* We lose statbuf here, so make sure response has been sent */ return 1; }
int process_get(request * req) { int bytes_written; volatile int bytes_to_write; bytes_to_write = req->filesize - req->filepos; if (bytes_to_write > SOCKETBUF_SIZE) bytes_to_write = SOCKETBUF_SIZE; if (sigsetjmp(env, 1) == 0) { handle_sigbus = 1; #ifdef SERVER_SSL if(req->ssl == NULL){ #endif /*SERVER_SSL*/ bytes_written = write(req->fd, req->data_mem + req->filepos, bytes_to_write); #ifdef SERVER_SSL }else{ bytes_written = SSL_write(req->ssl, req->data_mem + req->filepos, bytes_to_write); #if 0 printf("SSL_write\n"); #endif /*0*/ } #endif /*SERVER_SSL*/ handle_sigbus = 0; /* OK, SIGBUS **after** this point is very bad! */ } else { /* sigbus! */ log_error_doc(req); /* sending an error here is inappropriate * if we are here, the file is mmapped, and thus, * a content-length has been sent. If we send fewer bytes * the client knows there has been a problem. * We run the risk of accidentally sending the right number * of bytes (or a few too many) and the client * won't be the wiser. */ req->status = DEAD; fprintf(stderr, "%sGot SIGBUS in write(2)!\n", get_commonlog_time()); return 0; } if (bytes_written < 0) { if (errno == EWOULDBLOCK || errno == EAGAIN) return -1; /* request blocked at the pipe level, but keep going */ else { if (errno != EPIPE) { log_error_doc(req); /* Can generate lots of log entries, */ perror("write"); /* OK to disable if your logs get too big */ } req->status = DEAD; return 0; } } req->filepos += bytes_written; req->busy_flag = BUSY_FLAG_AUDIO|BUSY_FLAG_VIDEO; if (req->filepos == req->filesize) { /* EOF */ #ifdef DAVINCI_IPCAM if (req->http_stream == URI_STREAM_MJPEG) { #if 1 #if 0 while (audio_get(req) > 0); if (req->audio_length >= AUDIO_SEND_SIZE) { audio_send(req); return 1; } #else if (audio_get(req, FMT_MJPEG) > 0) return 1; #endif #else req->busy_flag &= ~BUSY_FLAG_AUDIO; #endif if (req->serial_lock) { GetAVData(AV_OP_UNLOCK_MJPEG, req->serial_lock, NULL); req->serial_lock = 0; } GetAVData(AV_OP_GET_MJPEG_SERIAL, -1, &req->av_data); if (req->av_data.serial < req->serial_book) { req->busy_flag &= ~BUSY_FLAG_VIDEO; return 1; } GetAVData(AV_OP_LOCK_MJPEG, req->av_data.serial, &req->av_data ); req->data_mem = req->av_data.ptr; req->filesize = req->av_data.size+16; req->filepos = 0; req->serial_lock = req->av_data.serial; req->serial_book = req->av_data.serial+1; reset_output_buffer(req); req_write(req, "\r\n"); print_mjpeg_headers(req); return 1; } if (req->http_stream == URI_STREAM_MPEG4 || req->http_stream == URI_STREAM_AVC) { int ret; #if 1 #if 0 while (audio_get(req) > 0); if (req->audio_length > AUDIO_SEND_SIZE) { req->busy_flag |= BUSY_FLAG_AUDIO; audio_send(req); return 1; } #else if (audio_get(req, FMT_MPEG4) > 0) return 1; #endif #else req->busy_flag &= ~BUSY_FLAG_AUDIO; #endif ret = GetAVData(AV_OP_LOCK_MP4, req->serial_book, &req->av_data); if (ret == RET_SUCCESS) { GetAVData(AV_OP_UNLOCK_MP4, req->serial_lock, NULL); req->data_mem = req->av_data.ptr; req->filesize = req->av_data.size+16; req->filepos = 0; req->serial_lock = req->av_data.serial; req->serial_book = req->av_data.serial+1; reset_output_buffer(req); req_write(req, "\r\n"); if (req->http_stream == URI_STREAM_AVC) { print_avc_headers(req); } else { print_mpeg4_headers(req); } return 1; } else if (ret == RET_NO_VALID_DATA) { req->busy_flag &= ~BUSY_FLAG_VIDEO; return 1; } else { GetAVData(AV_OP_GET_MPEG4_SERIAL, -1, &req->av_data ); req->serial_book = req->av_data.serial; dbg("lock error ret=%d\n", ret); return 1; } } if (req->http_stream == URI_STREAM_MPEG4CIF || req->http_stream == URI_STREAM_AVCCIF) { int ret; #if 1 #if 0 while (audio_get(req) > 0); if (req->audio_length > AUDIO_SEND_SIZE) { audio_send(req); return 1; } #else if (audio_get(req, FMT_MPEG4_EXT) > 0) return 1; #endif #else req->busy_flag &= ~BUSY_FLAG_AUDIO; #endif ret = GetAVData(AV_OP_LOCK_MP4_CIF, req->serial_book, &req->av_data); if (ret == RET_SUCCESS) { GetAVData(AV_OP_UNLOCK_MP4_CIF, req->serial_lock, NULL); req->data_mem = req->av_data.ptr; req->filesize = req->av_data.size+16; req->filepos = 0; req->serial_lock = req->av_data.serial; req->serial_book = req->av_data.serial+1; reset_output_buffer(req); req_write(req, "\r\n"); if (req->http_stream == URI_STREAM_AVCCIF) { print_avc_headers(req); } else { print_mpeg4_headers(req); } return 1; } else if (ret == RET_NO_VALID_DATA) { req->busy_flag &= ~BUSY_FLAG_VIDEO; return 1; } else { GetAVData(AV_OP_GET_MPEG4_CIF_SERIAL, -1, &req->av_data ); req->serial_book = req->av_data.serial; dbg("lock error ret=%d\n", ret); return 1; } } #endif // DAVINCI_IPCAM return 0; } else return 1; /* more to do */ }
int init_get(request * req) { int data_fd, saved_errno, dynamic=0; struct stat statbuf; volatile int bytes; data_fd = open(req->pathname, O_RDONLY); saved_errno = errno; /* might not get used */ #ifdef GUNZIP if (data_fd == -1 && errno == ENOENT) { /* cannot open */ /* it's either a gunzipped file or a directory */ char gzip_pathname[MAX_PATH_LENGTH]; int len; len = strlen(req->pathname); memcpy(gzip_pathname, req->pathname, len); memcpy(gzip_pathname + len, ".gz", 3); gzip_pathname[len + 3] = '\0'; data_fd = open(gzip_pathname, O_RDONLY); if (data_fd != -1) { close(data_fd); req->response_status = R_REQUEST_OK; if (req->pathname) free(req->pathname); req->pathname = strdup(gzip_pathname); if (!req->pathname) { log_error_time(); perror("strdup"); send_r_error(req); return 0; } if (!req->simple) { req_write(req, "HTTP/1.0 200 OK-GUNZIP\r\n"); print_http_headers(req); print_content_type(req); print_last_modified(req); req_write(req, "\r\n"); req_flush(req); } if (req->method == M_HEAD) return 0; return init_cgi(req); } } #endif if (data_fd == -1) { log_error_doc(req); errno = saved_errno; perror("document open"); if (saved_errno == ENOENT) send_r_not_found(req); else if (saved_errno == EACCES) send_r_forbidden(req); else send_r_bad_request(req); return 0; } fstat(data_fd, &statbuf); if (S_ISDIR(statbuf.st_mode)) { /* directory */ close(data_fd); /* close dir */ if (req->pathname[strlen(req->pathname) - 1] != '/') { char buffer[3 * MAX_PATH_LENGTH + 128]; if (server_port != 80) sprintf(buffer, "http://%s:%d%s/", server_name, server_port, req->request_uri); else sprintf(buffer, "http://%s%s/", server_name, req->request_uri); send_r_moved_perm(req, buffer); return 0; } data_fd = get_dir(req, &statbuf); /* updates statbuf */ if (data_fd == -1) /* couldn't do it */ return 0; /* errors reported by get_dir */ else if (data_fd <= 1) /* data_fd == 0 -> close it down, 1 -> continue */ return data_fd; /* else, data_fd contains the fd of the file... */ } #ifdef DAVINCI_IPCAM if (req->http_uri && (req->http_uri->uri_flag & URI_FLAG_NEED_PARSE)) dynamic = 1; #endif if (req->if_modified_since && !dynamic && !modified_since(&(statbuf.st_mtime), req->if_modified_since)) { send_r_not_modified(req); close(data_fd); return 0; } req->filesize = statbuf.st_size; req->last_modified = statbuf.st_mtime; if (req->method == M_HEAD || req->filesize == 0) { send_r_request_ok(req); close(data_fd); return 0; } if (req->filesize > MAX_FILE_MMAP) { send_r_request_ok(req); /* All's well */ req->status = PIPE_READ; req->cgi_status = CGI_BUFFER; req->data_fd = data_fd; req_flush(req); /* this should *always* complete due to the size of the I/O buffers */ req->header_line = req->header_end = req->buffer; return 1; } if (req->filesize == 0) { /* done */ send_r_request_ok(req); /* All's well *so far* */ close(data_fd); return 1; } /* NOTE: I (Jon Nelson) tried performing a read(2) * into the output buffer provided the file data would * fit, before mmapping, and if successful, writing that * and stopping there -- all to avoid the cost * of a mmap. Oddly, it was *slower* in benchmarks. */ req->mmap_entry_var = find_mmap(data_fd, &statbuf); if (req->mmap_entry_var == NULL) { req->buffer_end = 0; if (errno == ENOENT) send_r_not_found(req); else if (errno == EACCES) send_r_forbidden(req); else send_r_bad_request(req); close(data_fd); return 0; } req->data_mem = req->mmap_entry_var->mmap; close(data_fd); /* close data file */ if ((long) req->data_mem == -1) { boa_perror(req, "mmap"); return 0; } #ifdef DAVINCI_IPCAM if (dynamic) { char *addr = (char *)malloc(req->filesize + 1025); if (addr) { req->mem_flag |= MFLAG_IS_MEMORY; req->mmap_ptr = req->data_mem; req->mmap_size = req->filesize; memcpy(addr+1024, req->data_mem, req->filesize); addr[req->filesize+1024] = '\0'; req->data_mem = addr; req->filesize = html_argument_parse(req->authority, addr+1024, req->data_mem); send_request_ok_no_cache(req); /* All's well */ return 1; } } #endif // DAVINCI_IPCAM send_r_request_ok(req); /* All's well */ bytes = BUFFER_SIZE - req->buffer_end; /* bytes is now how much the buffer can hold * after the headers */ if (bytes > 0) { if (bytes > req->filesize) bytes = req->filesize; if (sigsetjmp(env, 1) == 0) { handle_sigbus = 1; memcpy(req->buffer + req->buffer_end, req->data_mem, bytes); handle_sigbus = 0; /* OK, SIGBUS **after** this point is very bad! */ } else { /* sigbus! */ log_error_doc(req); reset_output_buffer(req); send_r_error(req); fprintf(stderr, "%sGot SIGBUS in memcpy!\n", get_commonlog_time()); return 0; } req->buffer_end += bytes; req->filepos += bytes; if (req->filesize == req->filepos) { req_flush(req); req->status = DONE; } } /* We lose statbuf here, so make sure response has been sent */ return 1; }
int init_get(request * req) { int data_fd, saved_errno; struct stat statbuf; volatile int bytes; data_fd = open(req->pathname, O_RDONLY); saved_errno = errno; #ifdef GUNZIP if (data_fd == -1 && errno == ENOENT) { char gzip_pathname[MAX_PATH_LENGTH]; int len; len = strlen(req->pathname); memcpy(gzip_pathname, req->pathname, len); memcpy(gzip_pathname + len, ".gz", 3); gzip_pathname[len + 3] = '\0'; data_fd = open(gzip_pathname, O_RDONLY); if (data_fd != -1) { close(data_fd); req->response_status = R_REQUEST_OK; if (req->pathname) free(req->pathname); req->pathname = strdup(gzip_pathname); if (!req->pathname) { log_error_time(); perror("strdup"); send_r_error(req); return 0; } if (!req->simple) { req_write(req, "HTTP/1.0 200 OK-GUNZIP\r\n"); print_http_headers(req); print_content_type(req); print_last_modified(req); req_write(req, "\r\n"); req_flush(req); } if (req->method == M_HEAD) return 0; return init_cgi(req); } } #endif if (data_fd == -1) { log_error_doc(req); errno = saved_errno; perror("document open"); if (saved_errno == ENOENT) send_r_not_found(req); else if (saved_errno == EACCES) send_r_forbidden(req); else send_r_bad_request(req); return 0; } fstat(data_fd, &statbuf); if (S_ISDIR(statbuf.st_mode)) { close(data_fd); if (req->pathname[strlen(req->pathname) - 1] != '/') { char buffer[3 * MAX_PATH_LENGTH + 128]; if (server_port != 80) sprintf(buffer, "http://%s:%d%s/", server_name, server_port, req->request_uri); else sprintf(buffer, "http://%s%s/", server_name, req->request_uri); send_r_moved_perm(req, buffer); return 0; } data_fd = get_dir(req, &statbuf); if (data_fd == -1) return 0; else if (data_fd <= 1) return data_fd; } if (req->if_modified_since && !modified_since(&(statbuf.st_mtime), req->if_modified_since)) { send_r_not_modified(req); close(data_fd); return 0; } req->filesize = statbuf.st_size; req->last_modified = statbuf.st_mtime; if (req->method == M_HEAD || req->filesize == 0) { send_r_request_ok(req); close(data_fd); return 0; } if (req->filesize > MAX_FILE_MMAP) { send_r_request_ok(req); req->status = PIPE_READ; req->cgi_status = CGI_BUFFER; req->data_fd = data_fd; req_flush(req); /* this should *always* complete due to the size of the I/O buffers */ req->header_line = req->header_end = req->buffer; return 1; } if (req->filesize == 0) { send_r_request_ok(req); close(data_fd); return 1; } /* NOTE: I (Jon Nelson) tried performing a read(2) * into the output buffer provided the file data would * fit, before mmapping, and if successful, writing that * and stopping there -- all to avoid the cost * of a mmap. Oddly, it was *slower* in benchmarks. */ req->mmap_entry_var = find_mmap(data_fd, &statbuf); if (req->mmap_entry_var == NULL) { req->buffer_end = 0; if (errno == ENOENT) send_r_not_found(req); else if (errno == EACCES) send_r_forbidden(req); else send_r_bad_request(req); close(data_fd); return 0; } req->data_mem = req->mmap_entry_var->mmap; close(data_fd); if ((long) req->data_mem == -1) { boa_perror(req, "mmap"); return 0; } send_r_request_ok(req); bytes = BUFFER_SIZE - req->buffer_end; /* bytes is now how much the buffer can hold * after the headers */ if (bytes > 0) { if (bytes > req->filesize) bytes = req->filesize; if (sigsetjmp(env, 1) == 0) { handle_sigbus = 1; memcpy(req->buffer + req->buffer_end, req->data_mem, bytes); handle_sigbus = 0; } else { log_error_doc(req); reset_output_buffer(req); send_r_error(req); fprintf(stderr, "%sGot SIGBUS in memcpy!\n", get_commonlog_time()); return 0; } req->buffer_end += bytes; req->filepos += bytes; if (req->filesize == req->filepos) { req_flush(req); req->status = DONE; } } return 1; }
/* When the roving module receives a packet that starts with @, it passes the rest * of the command to this function. */ uint8_t parse_shemp_command(uint8_t * string, uint16_t length) { // TODO Make this entire function much more modular uint16_t cur_index = 0; if(string[cur_index] == 'W') { // Wait cur_index++; uint16_t wait_ms = read_number_from_string(&string[cur_index], 4); wait(wait_ms); // And then clear output buffer reset_output_buffer(); } else if(string[cur_index] == 'R') { // Relay cur_index++; if (string[cur_index] == 'N') { set_relay(ON); } else if (string[cur_index] == 'F') { set_relay(OFF); } cur_index++; } else if (string[cur_index] == 'H') { // Sync with header cur_index++; server_wants_header = TRUE; okay_to_transmit = FALSE; //transmit_header(); } else if(string[cur_index] == 'K') { // Okay to transmit cur_index++; server_wants_header = FALSE; okay_to_transmit = TRUE; } else if(string[cur_index] == 'S') { cur_index++; // Sensor configuration uint8_t sensor_loc = 0; uint8_t sensor_type = 0; uint8_t sensor_state = 0; time_ref sensor_period = 0; uint16_t sensor_size = 0; uint8_t sensor_index = 0; #define ENABLED 'E' #define DISABLED 'D' while(cur_index < length) { if(string[cur_index] == 'L') { cur_index++; // Location sensor_loc = string[cur_index]; cur_index++; } else if(string[cur_index] == 'T') { cur_index++; // Type sensor_type = string[cur_index]; cur_index++; } else if(string[cur_index] == 's') { cur_index++; // State if(string[cur_index] == 'E') { cur_index++; sensor_state = ENABLED; } else if (string[cur_index] == 'D') { cur_index++; sensor_state = DISABLED; } } else if(string[cur_index] == 'P') { cur_index++; sensor_period = new_time_from_string(&string[cur_index]); cur_index += STRING_TO_TIME_LENGTH; } else if(string[cur_index] == 'C') { cur_index++; sensor_size = read_number_from_string(&string[cur_index], 5); cur_index += 5; } } // Command is parsed, now to determine how to do it if (sensor_loc == 'I') { // Internal, internal voltage, current, or wattage if (sensor_type == 'W') { // Wattage can be enabled and disabled and period changed if (sensor_state == DISABLED) disable_internal_wattage_sensor(); else if (sensor_state == ENABLED) { // Else it should be enabled if (sensor_period && sensor_size) { // We have all the needed information create_internal_wattage_sensor(sensor_period, sensor_size); wait(1); //This ensures that all 3 sensors involved are in sync enable_internal_wattage_sensor(); } } } else if (sensor_type == 'V') { // Handle internal voltage sensor if (sensor_state == DISABLED) disable_internal_voltage_sensor(); else if (sensor_state == ENABLED) { // Else it should be enabled if (sensor_period && sensor_size) { // We have all the needed information create_internal_voltage_sensor(sensor_period, sensor_size); wait(1); enable_internal_voltage_sensor(); } } } else if (sensor_type == 'I') { // Handle internal current sensor if (sensor_state == DISABLED) disable_internal_current_sensor(); else if (sensor_state == ENABLED) { // Else it should be enabled if (sensor_period && sensor_size) { // We have all the needed information create_internal_current_sensor(sensor_period, sensor_size); wait(1); enable_internal_current_sensor(); } } } else if (sensor_type == 'T') { // Handle internal temperature sensor // It can be enabled, disabled, period and size changed if (sensor_state == DISABLED) disable_internal_temperature_sensor(); else if (sensor_state == ENABLED) { // Else it should be enabled if (sensor_period && sensor_size) { // We have all the needed information create_temperature_sensor('I',sensor_period, sensor_size); wait(1); enable_internal_temperature_sensor(); } } } } else if (sensor_loc == 'A' || sensor_loc == 'B') { sensor_index = sensor_loc - 'A'; if(sensor_state == DISABLED) { // Should disable that sensor if it matches if(sensor_type == sensor_get_type(aux_sensor[sensor_index])) { sensor_disable_this(aux_sensor[sensor_index]); } } else if (sensor_state == ENABLED) { if(sensor_period && sensor_size) { // We have everything we need, but maybe we are already set up, do we have to change anything? if(sensor_type != sensor_get_type(aux_sensor[sensor_index]) || time_cmp(sensor_period, sensor_get_period(aux_sensor[sensor_index])) || sensor_size != sensor_get_size(aux_sensor[sensor_index])) { // It is a different type // Or a different period // Or a different size sensor_delete_this(aux_sensor[sensor_index]); aux_sensor[sensor_index] = 0; } sensor_ref s; if(sensor_type == 'T') { // External Temperature Sensor s = create_temperature_sensor(sensor_loc, sensor_period, sensor_size); } else if(sensor_type == 'A') { // External Audio Sensor s = create_audio_sensor(sensor_loc, sensor_period, sensor_size); } else if(sensor_type == 'L') { // External Light Sensor s = create_light_sensor(sensor_loc, sensor_period, sensor_size); } if(s) { aux_sensor[sensor_index] = s; if(aux_sensor_enabled[sensor_index]) sensor_enable_this(aux_sensor[sensor_index]); } } } } time_delete(&sensor_period); } return SUCCESS; }